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
34 lines (29 loc) · 1.07 KB
/
cachematrix.R
File metadata and controls
34 lines (29 loc) · 1.07 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
## These functions help improving the performance of data manipulation by
## avoiding repeted calculation if already done before.
## This function returns a list of function to set and get the matrix
## and its inverse
makeCacheMatrix <- function(x = matrix()) {
invM <- matrix(data=NA,nrow = nrow(x),ncol = nrow(x))
set <- function(y) {
x <<- y
invM <<- matrix(data=NA,nrow = nrow(x),ncol = nrow(x))
}
get <- function() x
setInv <- function(z) invM <<- z
getInv <- function() invM
list(set = set, get = get,setInv = setInv,getInv = getInv)
}
## This function calculates the inverse of a matrix or returns its
## previus calculated value if it was calculated before
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invM <- x$getInv()
if (!all(is.na(invM))) {
message("getting cached data")
return(invM)
}
data <- x$get()
invM <- solve(data,...)
x$setInv(invM)
invM
}