forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
36 lines (31 loc) · 980 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
## Author: ama4tf
## The goal is to create an object that can cache the solution to a complex operation on a matrix (in this case the inverse)
##The second function is the process to check this cache and if not populated, populate it
## makeCaceMatrix creates a list object that can store a
## cached version of a matrix and it's inverse in the parent environment
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInv <- function(inv) m <<- inv
getInv <- function() m
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## Given the cached variable created above, looks for if getInv is set
## If yes returns it, if not, it solves thematrix and stores it as m using setInv
cacheSolve <- function(x, ...) {
m <- x$getInv()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setInv(m)
m
}