File tree Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Expand file tree Collapse file tree 1 file changed +26
-6
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
+ # # A pair of functions that cache the inverse of a matrix.
3
2
4
- # # Write a short comment describing this function
3
+ # # This function creates a special "matrix" object that can cache its inverse.
5
4
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ inv <- NULL
7
+ set <- function (y ) {
8
+ x <<- y
9
+ inv <<- NULL
10
+ }
11
+ get <- function () x
12
+ setinv <- function (inverse ) inv <<- inverse
13
+ getinv <- function () inv
14
+ list (set = set , get = get ,
15
+ setinv = setinv ,
16
+ getinv = getinv )
8
17
}
9
18
10
19
11
- # # Write a short comment describing this function
20
+ # # This function computes the inverse of the special "matrix" returned by
21
+ # # makeCacheMatrix above. If the inverse has already been calculated (and the
22
+ # # matrix has not changed), then cacheSolve retrieves the inverse from the
23
+ # # cache.
12
24
13
25
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
26
+ inv <- x $ getinv()
27
+ if (! is.null(inv )) {
28
+ message(" Getting cached data." )
29
+ return (inv )
30
+ }
31
+ data <- x $ get()
32
+ inv <- solve(data , ... )
33
+ x $ setinv(inv )
34
+ inv
15
35
}
You can’t perform that action at this time.
0 commit comments