File tree Expand file tree Collapse file tree 1 file changed +26
-7
lines changed Expand file tree Collapse file tree 1 file changed +26
-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
3
-
4
- # # Write a short comment describing this function
1
+ # # A set of functions to create a matrix with a cached value of the inverse
2
+ # # Use the function cacheSolve to use the cached value if it exists
5
3
4
+ # # Make a matrix with a function to cache the inverse value
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ cached_inverse <- NULL
7
+ set <- function (y ) {
8
+ x <<- y
9
+ cached_inverse <<- NULL
10
+ }
11
+ get <- function () x
12
+ setinverse <- function (inverse ) cached_inverse <<- inverse
13
+ getinverse <- function () cached_inverse
14
+ list (set = set , get = get ,
15
+ setinverse = setinverse ,
16
+ getinverse = getinverse )
8
17
}
9
18
10
19
11
- # # Write a short comment describing this function
20
+ # # Returns a matrix that is the inverse of 'x',
21
+ # # using the cached value in x if available
22
+ # # Note: x should be an object created with makeCacheMatrix, it can not be an ordinary matrix
12
23
13
24
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
25
+ cached_inverse <- x $ getinverse()
26
+ if (! is.null(cached_inverse )) {
27
+ message(" getting cached data" )
28
+ return (cached_inverse )
29
+ }
30
+ data <- x $ get()
31
+ inv <- solve(data , ... )
32
+ x $ setinverse(inv )
33
+ inv
15
34
}
You can’t perform that action at this time.
0 commit comments