File tree 1 file changed +21
-2
lines changed
1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 4
4
# # Write a short comment describing this function
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ m <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ m <<- NULL
11
+ }
12
+ get <- function () x
13
+ setInverse <- function (inverse ) m <<- inverse
14
+ getInverse <- function () m
15
+ list (set = set , get = get ,
16
+ setInverse = setInverse ,
17
+ getInverse = getInverse )
8
18
}
9
19
10
20
11
21
# # Write a short comment describing this function
12
22
13
23
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
24
+ m <- x $ getInverse() # query the x vector's cache
25
+ if (! is.null(m )) { # if there is a cache
26
+ message(" getting cached data" )
27
+ return (m ) # just return the cache, no computation needed
28
+ }
29
+ data <- x $ get() # if there's no cache
30
+ m <- solve(data , ... ) # we actually compute them here
31
+ x $ setInverse(m ) # save the result back to x's cache
32
+ m # return the result
33
+ # # Return a matrix that is the inverse of 'x'
15
34
}
You can’t perform that action at this time.
0 commit comments