File tree Expand file tree Collapse file tree 1 file changed +20
-7
lines changed Expand file tree Collapse file tree 1 file changed +20
-7
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
+ # # Functions to solve the inverse of a matrix and cache the result.
3
2
4
- # # Write a short comment describing this function
3
+ # # Creates a container that can hold a matrix and its cached inverse.
5
4
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ xinv <- NULL
7
+ set <- function (y ) {
8
+ xinv <<- NULL
9
+ x <<- y
10
+ }
11
+ list (set = set , get = function () x ,
12
+ setinverse = function (y ) xinv <<- y , getinverse = function () xinv )
8
13
}
9
14
10
-
11
- # # Write a short comment describing this function
15
+ # # Solves and caches the inverse of the matrix, so that repeated calls return the cached result until the matrix is changed.
12
16
13
17
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
18
+ xinv <- x $ getinverse()
19
+ if (is.null(xinv )) {
20
+ message(" solving for the inverse" )
21
+ xinv <- solve(x $ get(), ... )
22
+ x $ setinverse(xinv )
23
+ } else {
24
+ message(" using cached solution" )
25
+ }
26
+ xinv
15
27
}
28
+
You can’t perform that action at this time.
0 commit comments