File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## This function creates a special "matrix" object that can cache its inverse.
2
+ makeCacheMatrix <- function(x = matrix()) {
3
+ inv <- NULL # initialize inverse as NULL
4
+ set <- function(y) {
5
+ x <<- y # assign new matrix value
6
+ inv <<- NULL # reset inverse cache
7
+ }
8
+ get <- function() x
9
+ setinverse <- function(inverse) inv <<- inverse
10
+ getinverse <- function() inv
11
+ list(set = set, get = get,
12
+ setinverse = setinverse,
13
+ getinverse = getinverse)
14
+ }
15
+ ## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
16
+ cacheSolve <- function(x, ...) {
17
+ inv <- x$getinverse()
18
+ if(!is.null(inv)) {
19
+ message("getting cached data")
20
+ return(inv) # return cached inverse
21
+ }
22
+ data <- x$get()
23
+ inv <- solve(data, ...) # compute inverse
24
+ x$setinverse(inv) # cache the inverse
25
+ inv
26
+ }
You can’t perform that action at this time.
0 commit comments