forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
36 lines (31 loc) · 952 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
36
## These functions allow for the creation of matrices and quickly
## storing/accessing their inverses.
## Creates a special "matrix", actually a list that contains getter
## and setter functions for both the matrix data and its inverse.
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() {x}
setInverse <- function(i) {inverse <<- i}
getInverse <- function() {inverse}
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Given a special matrix, returns its inverse.
## The inverse returned is taken from the cache (if already exists)
## or computed and then cached (if it doesn't yet).
cacheSolve <- function(x, ...) {
inverse <- x$getInverse()
if(!is.null(inverse)) {
message("getting cached data")
return(inverse)
}
data <- x$get()
inverse <- solve(data)
x$setInverse(inverse)
inverse
}