Skip to content

Commit 328802b

Browse files
author
Gregg Meluski
committed
updated cache matrix file
1 parent 7f657dd commit 328802b

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

cachematrix.R

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,52 @@
55

66
makeCacheMatrix <- function(x = matrix()) {
77

8+
inverse <- NULL
9+
set <- function (y) {
10+
x <<- y
11+
inverse <<- NULL
12+
}
13+
14+
get <- function () {
15+
x
16+
}
17+
18+
setInverse <- function (passedInverse) {
19+
inverse <<- passedInverse
20+
}
21+
22+
getInverse <- function () {
23+
inverse
24+
}
25+
26+
list(
27+
set = set,
28+
get = get,
29+
setInverse = setInverse,
30+
getInverse = getInverse
31+
)
832
}
933

1034

1135
## Write a short comment describing this function
36+
# this function will handle the cacheMatrix object, and either
37+
# retrieve a cached inverse for the current matrix, or create
38+
# a new inverse and store it
1239

1340
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
41+
## Return a matrix that is the inverse of 'x'
42+
inverse <- x$getInverse()
43+
44+
# there's already a cached inverse
45+
if(!is.null(inverse)) {
46+
message('getting cached data')
47+
return(inverse)
48+
}
49+
50+
# if not, get the original value of x, invert it and store it
51+
data <- x$get()
52+
inverse <- solve(data, ...)
53+
x$setInverse(inverse)
54+
inverse
55+
1556
}

0 commit comments

Comments
 (0)