File tree Expand file tree Collapse file tree 1 file changed +26
-5
lines changed Expand file tree Collapse file tree 1 file changed +26
-5
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
+ # # These two functions can be used to cache the inverse of a matrix.
2
+ # # Create the matrix with the makeCacheMatrix function and then compute
3
+ # # the inverse with the cacheSolve function.
3
4
4
- # # Write a short comment describing this function
5
+ # # This function wraps a matrix so that it can be used with the cacheSolve function.
5
6
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ inv <- NULL
9
+ set <- function (y ) {
10
+ x <<- y
11
+ inverse <<- NULL
12
+ }
13
+ get <- function () x
14
+ setinverse <- function (inverse ) inv <<- inverse
15
+ getinverse <- function () inv
16
+ list (set = set , get = get ,
17
+ setinverse = setinverse ,
18
+ getinverse = getinverse )
8
19
}
9
20
10
21
11
- # # Write a short comment describing this function
22
+ # # This function returns the inverse of the given matrix and caches the result
23
+ # # to speed up any future calls.
12
24
13
25
cacheSolve <- function (x , ... ) {
14
26
# # Return a matrix that is the inverse of 'x'
27
+ inv <- x $ getinverse()
28
+ if (! is.null(inv )) {
29
+ message(" getting cached data" )
30
+ return (inv )
31
+ }
32
+ data <- x $ get()
33
+ inv <- solve(data , ... )
34
+ x $ setmean(inv )
35
+ inv
15
36
}
You can’t perform that action at this time.
0 commit comments