Skip to content

Commit 04dcdc0

Browse files
committed
Update cachematrix.R
1 parent 7f657dd commit 04dcdc0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cachematrix.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,28 @@
33

44
## Write a short comment describing this function
55

6-
makeCacheMatrix <- function(x = matrix()) {
7-
6+
makeCacheMatrix <- function(mtx = matrix()) {
7+
inverse <- NULL
8+
set <- function(x) {
9+
mtx <<- x;
10+
inverse <<- NULL;
11+
}
12+
get <- function() return(mtx);
13+
setinv <- function(inv) inverse <<- inv;
14+
getinv <- function() return(inverse);
15+
return(list(set = set, get = get, setinv = setinv, getinv = getinv))
816
}
917

10-
1118
## Write a short comment describing this function
1219

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
20+
cacheSolve <- function(mtx, ...) {
21+
inverse <- mtx$getinv()
22+
if(!is.null(inverse)) {
23+
message("Getting cached data...")
24+
return(inverse)
25+
}
26+
data <- mtx$get()
27+
invserse <- solve(data, ...)
28+
mtx$setinv(inverse)
29+
return(inverse)
1530
}

0 commit comments

Comments
 (0)