File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -34,8 +34,26 @@ makeCacheMatrix <- function(x = matrix()) {
34
34
}
35
35
36
36
37
- # # Write a short comment describing this function
38
-
37
+ # This function returns an inverse of the matrix. If the inverse is not already
38
+ # computed, it will compute it and store in cache; otherwise, it will just return the value from cache
39
+ # x must be the output of the makeCacheMatrix function
39
40
cacheSolve <- function (x , ... ) {
40
- # # Return a matrix that is the inverse of 'x'
41
+ # try to get cached value
42
+ inverse <- x $ getinverse()
43
+
44
+ # verify if inverse has ever been cached (NULL indicates it has not been computed yet)
45
+ # if it has been computed; return cached value
46
+ if (! is.null(inverse )) {
47
+ message(" getting cached data" )
48
+ return (inverse )
49
+ }
50
+
51
+ # otherwise, compute the inverse and store in cache
52
+ computedInverse <- solve(x $ get())
53
+
54
+ # store computed value in cache
55
+ x $ setinverse(computedInverse )
56
+
57
+ # return the value
58
+ computedInverse
41
59
}
You can’t perform that action at this time.
0 commit comments