File tree Expand file tree Collapse file tree 1 file changed +29
-12
lines changed Expand file tree Collapse file tree 1 file changed +29
-12
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
5
-
1
+ # makeCacheMatrix creates a list containing a function to
2
+ # 1. set the value of the matrix
3
+ # 2. get the value of the matrix
4
+ # 3. set the value of inverse of the matrix
5
+ # 4. get the value of inverse of the matrix
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ inv <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ inv <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinverse <- function (inverse ) inv <<- inverse
14
+ getinverse <- function () inv
15
+ list (set = set , get = get , setinverse = setinverse , getinverse = getinverse )
8
16
}
9
-
10
-
11
- # # Write a short comment describing this function
12
-
17
+ # The following function returns the inverse of the matrix. It first checks if
18
+ # the inverse has already been computed. If so, it gets the result and skips the
19
+ # computation. If not, it computes the inverse, sets the value in the cache via
20
+ # setinverse function.
21
+ # This function assumes that the matrix is always invertible.
13
22
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
15
- }
23
+ inv <- x $ getinverse()
24
+ if (! is.null(inv )) {
25
+ message(" getting cached data." )
26
+ return (inv )
27
+ }
28
+ data <- x $ get()
29
+ inv <- solve(data )
30
+ x $ setinverse(inv )
31
+ inv
32
+ }
You can’t perform that action at this time.
0 commit comments