forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
39 lines (33 loc) · 1.1 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
## The first function takes a matrix as input, calculates inverse and produces
## a list containing functions that will; set, get, set the inverse, get the
## inverse matrix. The second function takes the produced list and will
## produce the cached inverse matrix.
## This function takes in a matrix and outputs a list of functions to be
## used to solve the inverse matrix
makeCacheMatrix <- function(x = numeric()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinv <- function(inverse) inv <<- inverse
getinv <- function() inv
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## This function takes the a list. If the matrix has already been calculated
## it will just retrieve the cached data, but if not it will execute the
## functions that calculate the inverse matrix.
cacheSolve <- function(x, ...) {
inv <- x$getinv()
if(!is.null(inv)) {
message("Retrieving Cached Data")
return(inv)
}
matrix <- x$get()
inv <- solve(matrix, ...)
x$setinv(inv)
inv
}