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
48 lines (36 loc) · 1.16 KB
/
cachematrix.R
File metadata and controls
48 lines (36 loc) · 1.16 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
45
46
47
48
## Function to get/set a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
## place holder for the value of inverse
inv <- NULL
## function to set the matrix value to passed matrix and its inverse to null
set <- function(y) {
x <<- y
inv <<- NULL
}
## function to retrieve the currently stored matrix
get <- function() x
## resets the inverse to the passed value
setInv <- function(inverse) inv <<- inverse
## function that retrieves the currently stored inverse
getInv <- function() inv
## returns a list of functions
list(set = set, get = get, setInv = setInv, getInv = getInv)
}
## function to get the inverse of a matrix
cacheSolve <- function(x, ...) {
## Return the inverse of 'x'
inv <- x$getInv()
## if the inverse is stored in cache, return it
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
## get the matrix x
data <- x$get()
## perform the inverse calculation
inv <- solve(data, ...)
## store the inverse in cache
x$setInv(inv)
## return the inverse
inv
}