Skip to content

Commit fd92549

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

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cachematrix.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7+
m <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
m <<- NULL
11+
}
12+
get <- function() x
13+
setinverse <- function(solve) m <<- solve
14+
getinverse <- function() m
15+
list(set = set, get = get,
16+
setinverse = setinverse,
17+
getinverse = getinverse)
718

819
}
920

@@ -12,4 +23,13 @@ makeCacheMatrix <- function(x = matrix()) {
1223

1324
cacheSolve <- function(x, ...) {
1425
## Return a matrix that is the inverse of 'x'
26+
m <- x$getinverse()
27+
if(!is.null(m)) {
28+
message("getting cached data")
29+
return(m)
30+
}
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setinverse(m)
34+
m
1535
}

0 commit comments

Comments
 (0)