forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
32 lines (27 loc) · 1.02 KB
/
cachematrix.R
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
## makeCacheMatrix is for making a matrix that can store the inverse of a matrix so that other can use it convenient
## cacheSolve is to get the inverse of a matrix (if it is not stored in cache, then calculate, otherwise we just get data from cache)
## a list of function including get, set, getInv, setInv (i.e. getMatrix, setMatrix, getInverseOfMatrix, setInverseOfMatrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInv <- function(i) inv <<- i
getInv <- function() inv
list(set = set, get = get, setInv = setInv, getInv = getInv)
}
## getInverse of a special matrix (if its inverse is calculated, just return. otherwise calculate it)
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInv()
if (!is.null(inv)) {
message("getting cached matrix")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setInv(inv)
inv
}