Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 391d012

Browse files
committed
Update rdpeng#1
1 parent 7f657dd commit 391d012

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

cachematrix.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,35 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
MyInv <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
MyInv <<- NULL
11+
}
12+
get <- function() x
13+
setInverse <- function(inverse) MyInv <<- inverse #calculate the inverse
14+
getInverse <- function() MyInv
15+
list(set = set,
16+
get = get,
17+
setInverse = setInverse,
18+
getInverse = getInverse)
19+
820
}
921

1022

1123
## Write a short comment describing this function
1224

1325
cacheSolve <- function(x, ...) {
1426
## Return a matrix that is the inverse of 'x'
27+
MyInv <- x$getInverse()
28+
if (!is.null(MyInv)) {
29+
message("getting cached data")
30+
return(MyInv)
31+
}
32+
mat <- x$get()
33+
MyInv <- solve(mat, ...)
34+
x$setInverse(MyInv)
35+
MyInv
36+
1537
}
38+

0 commit comments

Comments
 (0)