forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
43 lines (31 loc) · 915 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
37
38
39
40
41
42
43
#The following two functions are a way of using the scoping rules in R to
#create a cacheable version of a Matrix's inverse.
#makeCaheMatrix takes a matrix argument, calculates its inverse, and caches
#the result.
makeCacheMatrix <- function(u = matrix()){
minv <- NULL
set <- function(y) {
u <<- y
minv <<- NULL
}
get <- function() u
setinv <- function(solve) minv <<- solve
getinv <- function() minv
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
#This functions solves for the inverse of the Matrix created by makeCacheMatrix.
#if no value is stored, the solve() function will run, otherwise it will return the value
#of minv
cacheSolve <- function(u, ...){
minv <- u$getinv()
if(!is.null(minv)) {
message("retrieving cached matrix")
return(minv)
}
data <- u$get()
minv <- solve(data, ...)
u$setinv(minv)
minv
}