forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
34 lines (29 loc) · 965 Bytes
/
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
33
34
## Provides 2 functions that being used together can cache the inverse of a matrix.
## NOTE : support only invertible matrix.
## Creates a list of fuctions that caches given the inverse of the given matrix object
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInv <- function(inverse) inv <<- inverse
getInv <- function() inv
list(get=get, set=set, getInv=getInv, setInv=setInv)
}
## Computes the inverse of the object returned by makeCacheMatrix above.
## If the inverse has already been calculated return the cached inverse matrix.
## If not, calcuate the inverse matrix and return it.
cacheSolve <- function(x, ...) {
inv <- x$getInv()
if (!is.null(inv)) {
message("getting cache data")
return(inv)
}
message("calculating inverse")
data <- x$get()
inv <- solve(data)
x$setInv(inv)
inv
}