forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
49 lines (45 loc) · 1.34 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## Caching allows to store data temporarily so to
## reduce computational costs and improve performance.
## In this exercise the concept is applied to the
## function solve. By solving the function only when
## necessary, recomputation is avoided.
## This function creates a list containing
## subfunctions to setting the values of the
## matrix, getting the value, inverting the matrix
## and getting the inverted matrix.
## This special matrix can cache its inverse.
## Example of testing:
## x <- makeCacheMatrix(matrix(c(4,3,3,2), ncol=2))
##cacheSolve(x)
## [,1] [,2]
##[1,] -2 3
##[2,] 3 -4
makeCacheMatrix <- function(x = matrix()) {
M <- NULL
setInv <- function(y) {
x <<- y
M <<- NULL
}
getInv <- function() x
setinverse <- function(solve) M <<- solve
getinverse <- function() M
list(setInv = setInv, getInv = getInv,
setinverse = setinverse,
getinverse = getinverse)
}
## Write a short comment describing this function
## This function uses the output of the previous
## function to check if the inverse has already been
## calculated. If yes, the result is retrieved,
## if not, it is calculated.
cacheSolve <- function(x, ...) {
M <- x$getinverse()
if(!is.null(M)) {
message("getting cached data")
return(m)
}
data <- x$getInv()
M <- solve(data, ...)
x$setinverse(M)
M
}