forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
32 lines (25 loc) · 893 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
## cachematrix.R contains functions to calculate the inverse of a matrix, using cache to store
## inverse matrix already calculated.
## makeCacheMatrix create a special matrix with methods to get and set the original matrix, and to set
## and get the inverse matrix.
## it returns a "special matrix" contained the methods described above"
makeCacheMatrix <- function(x = matrix()) {
mat.inv <- NULL
set <- function(y){
x<<-y
}
get <- function() x
setInv <- function(inv) {
mat.inv <<- inv
}
getInv <- function() mat.inv
list(set=set, get=get, setInv=setInv, getInv=getInv)
}
## cacheSolve calculates the inverse of the matrix, using cached valued of it if previously calculated.
## it returns the inverse matrix of the special matrix passed.
cacheSolve <- function(x, ...) {
if(is.null(x$getInv())){
x$setInv(solve(x$get()), ...)
}
x$getInv()
}