forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
36 lines (33 loc) · 1.13 KB
/
cachematrix.R
File metadata and controls
36 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## makeCacheMatrix(x): make a cacheable matrix
## arguments:
## x: a matrix
## return: a list represent the cacheable matrix
makeCacheMatrix <- function(x = matrix()) {
## cache for the inverseMatrix
inverseMatrix <- NULL
set <- function(y) {
x <<- y
inverseMatrix <<- NULL
}
get <- function() x
setInverseMatrix <- function(i) inverseMatrix <<- i
getInverseMatrix <- function() inverseMatrix
list(set = set, get = get,
setInverseMatrix = setInverseMatrix,
getInverseMatrix = getInverseMatrix)
}
## cacheSolve(x, ...): solve the cacheable matrix
## arguments:
## x: a cacheable matrix returned by makeCacheMatrix()
## return: inverse of x
cacheSolve <- function(x, ...) {
inverseMatrix <- x$getInverseMatrix()
if(!is.null(inverseMatrix)) {
message("getting cached data")
return(inverseMatrix)
}
matrix <- x$get()
inverseMatrix <- solve(matrix, ...)
x$setInverseMatrix(inverseMatrix)
inverseMatrix
}