File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
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
1
+ # # Given an invertible matrix compute the inverse and cache the result. Subsequent requests to get the inverse
2
+ # # of the same matrix will return the cache result instead of re-calculating.
3
3
4
- # # Write a short comment describing this function
4
+ # # This function creates a special "matrix" object that can cache its inverse.
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ i <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ i <<- NULL
11
+ }
12
+ get <- function () x
13
+ setinv <- function (inv ) i <<- inv
14
+ getinv <- function () i
15
+ list (set = set , get = get , setinv = setinv , getinv = getinv )
8
16
}
9
17
10
18
11
- # # Write a short comment describing this function
19
+ # # This function computes and caches the inverse of the special "matrix" returned by makeCacheMatrix above.
12
20
13
21
cacheSolve <- function (x , ... ) {
14
22
# # Return a matrix that is the inverse of 'x'
23
+ i <- x $ getinv()
24
+ if (! is.null(i )) {
25
+ message(" getting cached data" )
26
+ return (i )
27
+ }
28
+ data <- x $ get()
29
+ i <- solve(data , ... )
30
+ x $ setinv(i )
31
+ i
15
32
}
You can’t perform that action at this time.
0 commit comments