forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
40 lines (35 loc) · 1.31 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
## Computing the inverse of a matrix can be a costly operation.
## The functions below provide a system to cache the inverse of a matrix for future reference.
## This will prevent repeated calculations of the same inverse.
## The makeCacheMatrix function creates a special matrix, which is really a list containing functions to
## - set the value of the matrix
## - get the value of the matrix
## - set the value of the inverse matrix
## - get the value of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The cacheSolve function will try to get an inversed matrix from the cache of the special matrix.
## If no inversed matrix is yet calculated, it will do so and cache the result in the special matrix.
cacheSolve <- function(x, ...) {
i <- x$getInverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
data <- x$get()
i <- solve(data, ...)
x$setInverse(i)
i
}