forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
35 lines (31 loc) · 937 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
33
34
35
##Create a matrix that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
matrixInverse <- NULL
set <- function(y) {
x <<- y
matrixInverse <<- NULL
}
get <- function() x
setInverse <- function(inverse) matrixInverse <<- inverse
getInverse <- function() matrixInverse
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Computes the inverse of the matrix if
##i. The matrix has changed or
##ii. Matrix's inverse has not already been calculated.
##Else calculates inverse and caches(sets) it.
cacheSolve <- function(y, ...) {
## Return a matrix that is the inverse of 'x'
matrixInverse <- y$getInverse()
if (!is.null(matrixInverse)) {
message("Returned Cached Data as Inverse was already calculated")
return(matrixInverse)
}
matrix <- y$get()
matrixInverse <- solve(matrix, ...)
y$setInverse(matrixInverse)
matrixInverse
}