Skip to content

Commit 8770be5

Browse files
author
Chen He
committed
Commit to the homework2 in week3 from Chen He
1 parent 7f657dd commit 8770be5

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cachematrix.R

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@
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+
13+
get <- function() x
14+
setInverMatrix <- function(inverseMatrix) m <<- inverseMatrix
15+
getInverMatrix <- function() m
716

17+
list(set = set, get = get,
18+
setInverMatrix = setInverMatrix,
19+
getInverMatrix = getInverMatrix)
820
}
921

10-
1122
## Write a short comment describing this function
12-
1323
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
24+
25+
## Return a matrix that is the inverse of 'x'
26+
m <- x$getInverMatrix()
27+
if(!is.null(m)) {
28+
message("getting cached Matrix")
29+
return(m)
30+
}
31+
data <- x$get()
32+
m <- solve(data, ...)
33+
x$setInverMatrix(m)
34+
m
1535
}
36+

0 commit comments

Comments
 (0)