forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
44 lines (30 loc) · 1.35 KB
/
cachematrix.R
File metadata and controls
44 lines (30 loc) · 1.35 KB
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
## This file is the solution to programming assignment 2 and contains two functions.
## Make a list of functions that work on a captured varible.
## Store a matrix and a cached version of the inverse of the same matrix.
## No computation in this function, only getter & setter functions that work on the captured variable.
makeCacheMatrix <- function(x = matrix()) {
# Store the cached inverse matrix "inside" this closure (stored in the environment)
cache <- NULL
# Getter / setter functions to return
get <- function() x
getInverse <- function() cache
setInverse <- function(m) cache <<- m
# Return a list of functions, like the example code
list(get = get, getInverse = getInverse, setInverse = setInverse)
}
## Solve the inverse of a matrix, using the closure we created with makeCacheMatrix
## If the cache is set return that.
## If not, solve() for matrix and return the result after setting the cache
cacheSolve <- function(x, ...) {
inv <- x$getInverse() # the varibale we will return after setting it to the correct matrix
if(is.null(inv)){
# Nothing cached, solve and fill the cache
matrix <- x$get()
inv <- solve(matrix)
x$setInverse(inv)
} else {
# Got cached result, nothing to do
message("getting cached data")
}
inv
}