forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
50 lines (47 loc) · 1.64 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
50
## The script file implements two functions.
## The first builds a spacial "matrix" able to cache its inverse.
## The second computes the inverse of the spcial matrix if this isn't cached,
## otherwise returns the precalculated inverse matrix.
## The function builds a special "matrix" able to cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
## Sets the matrix to invert initializing the cached invertedMatrix to NULL.
set <- function(y){
x <<- y
inverse <<- NULL
}
## Returns the matrix to invert.
get <- function(){
x
}
## Sets the corresponding inverted matrix.
setInverse <- function(inv){
inverse <<- inv
}
## Returns the corresponding inverted matrix if cached, NULL otherwise.
getInverse <- function(){
inverse
}
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Computes the inverse of the special "matrix" if it's not cached or is changed
## and chache it, otherwise returns the cached inverse matrix.
## The function assumes that the input matrix is invertible.
cacheSolve <- function(x, ...) {
## Retrieve the inverted matrix value from the cachableMatrix object
invM <- x$getInverse()
## If the inverted matrix it's not in the cache or it's changed,
## then computes it and adds it to the chache.
if(is.null(invM)){
mToInvert <- x$get()
##Compute the inverted matrix
invM <- solve(mToInvert)
x$setInverse(invM)
}else{
message("getting cached data")
}
## Return a matrix that is the inverse of 'x'
invM
}