forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
46 lines (41 loc) · 1.4 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
## Two Functions - makeCacheMatrix, and cacheSolve
## Designed to solve and store the inverse of an invertable matrix
## to reduce required computation
## makeCacheMatrix creates a matrix object with four functions and stores
## a matrix and its inverse
## "set(mtx)" will defines the value of the matrix (as "mtx") after initial construction
## (and reset invert matrix data)
## "get()" returns the value of the matrix
## "setInv(inverse)" sets the value of the invert matrix to "inverse"
## "getInv()" returns the value of the invert matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(mtx) {
x <<- mtx
inv <<- NULL
}
get <- function() x
setInv <- function(inverse){
inv <<- inverse
}
getInv <- function() inv
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## Given an object of makeCacheMatrix, cacheSolve solves for the inverse
## then stores the invert matrix back into the makeCacheMatrix object.
## First check if inverse stored, if so, print return inverse
## else get() data from mCM object, then solve() using that data
## setInv() in the mCM object and return inverse
cacheSolve <- function(x,...) {
inv <- x$getInv()
if(!is.null(inv)){
message("Getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data,...)
x$setInv(inv)
inv
}