Skip to content

Commit 541eb08

Browse files
Update cachematrix.R
1 parent 7f657dd commit 541eb08

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

cachematrix.R

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
61
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
2+
inv <- NULL
3+
set <- function(y) {
4+
x <<- y
5+
inv <<- NULL
6+
}
7+
get <- function() x
8+
setinv <- function(inverse) inv <<- inverse
9+
getinv <- function() inv
10+
list(set = set, get = get, setinv = setinv, getinv = getinv)
11+
}
12+
## cacheSolve is a function which computes the inverse of the special "matrix"
13+
## returned by makeCacheMatrix above. If the inverse has already been calculated
14+
## (and the matrix has not changed), then the cachesolve should retrieve the
15+
## inverse from the cache
1316
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
17+
## Return a matrix that is the inverse of 'x'
18+
inv <- x$getinv()
19+
if(!is.null(inv)) {
20+
message("getting cached result")
21+
return(inv)
22+
}
23+
data <- x$get()
24+
inv <- solve(data, ...)
25+
x$setinv(inv)
26+
inv
27+
}

0 commit comments

Comments
 (0)