File tree Expand file tree Collapse file tree 1 file changed +27
-6
lines changed Expand file tree Collapse file tree 1 file changed +27
-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
+ # # makeCacheMatrix() creates a special "matrix" object that
2
+ # # can cache its inverse.
3
+ # # cacheSolve() returns the inverse of such a special "matrix".
3
4
4
- # # Write a short comment describing this function
5
5
6
+ # # Creates a "matrix" that can cache its own inverse.
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ inv <- NULL
9
+ set <- function ( y ) {
10
+ x <<- y
11
+ inv <<- NULL
12
+ }
13
+ get <- function () x
14
+ setinv <- function ( invv ) inv <<- invv
15
+ getinv <- function () inv
16
+ list ( set = set , get = get ,
17
+ setinv = setinv ,
18
+ getinv = getinv )
8
19
}
9
20
10
21
11
- # # Write a short comment describing this function
12
-
22
+ # # Returns the inverse of the special matrix 'x', either from
23
+ # # cache (if available), or by computing it.
24
+ # # ASSUME that the matrix supplied is always invertible.
13
25
cacheSolve <- function (x , ... ) {
14
26
# # Return a matrix that is the inverse of 'x'
27
+ inv <- x $ getinv()
28
+ if ( ! is.null(inv ) ) {
29
+ message(" Getting cached data..." )
30
+ return ( inv )
31
+ }
32
+ data <- x $ get()
33
+ inv <- solve( data )
34
+ x $ setinv( inv )
35
+ inv
15
36
}
You can’t perform that action at this time.
0 commit comments