forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
46 lines (35 loc) · 1.11 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
## This script contains two functions that enable the caching of an inverse matrix
## and returns its value if one exists, otherwise it returns the uncached inverse
## matrix and caches it.
## Create the cached inverse matrix.
makeCacheMatrix <- function(x = matrix()) {
setMatrix <- function(y) {
x <<- y
s <<- NULL
}
getMatrix <- function() x
setInverse <- function(x) s <<- solve(x)
getInverse <- function() s
list(setMatrix = setMatrix,
getMatrix = getMatrix,
setInverse = setInverse,
getInverse = getInverse)
}
## Solve for an inverse matrix and retrieved the cached version if one exists.
## Otherwise cache the value of the newly solved inverse matrix and return its
## value.
cacheSolve <- function(x, ...) {
s <- x$getInverse()
## Check if the inverse has already been cached and return
## it, if it has.
if(!is.null(s)) {
message("getting cached data")
return(s)
}
## Otherwise get the matrix, find the inverse, cache it and return its
## value.
data <- x$getMatrix()
inverse <- solve(data)
x$setInverse(inverse)
inverse
}