File tree Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Expand file tree Collapse file tree 1 file changed +18
-8
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
+ # # Invert a matrix and cache the result, to demonstrate the power of
2
+ # # environments in R!
3
3
4
- # # Write a short comment describing this function
5
-
6
- makeCacheMatrix <- function (x = matrix ()) {
4
+ # # Construct a special "matrix" object that can cache its own inverse.
7
5
6
+ makeCacheMatrix <- function (x = matrix ()) {
7
+ inverse <- NULL
8
+ list (get = function () x ,
9
+ getinverse = function () inverse ,
10
+ setinverse = function (inverse ) inverse <<- inverse )
8
11
}
9
12
10
13
11
- # # Write a short comment describing this function
14
+ # # Compute the inverse of a special "matrix" returned by
15
+ # # makeCacheMatrix(), above. Only calculate the result once -- stow the
16
+ # # result for next time and just return the cached value, if available.
12
17
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
18
+ cacheSolve <- function (x ) {
19
+ inverse <- x $ getinverse()
20
+ if (is.null(inverse )) {
21
+ x $ setinverse(solve(x $ get()))
22
+ } else {
23
+ inverse
24
+ }
15
25
}
You can’t perform that action at this time.
0 commit comments