Skip to content

Commit 95aa466

Browse files
committed
Caching a matrix inverse
1 parent 7f657dd commit 95aa466

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

cachematrix.R

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
31

4-
## Write a short comment describing this function
2+
## Creates a wrapper around a a matrix
53

6-
makeCacheMatrix <- function(x = matrix()) {
4+
makeCacheMatrix <- function(input = matrix()) {
5+
inverse <- NULL
6+
set <- function(y) {
7+
input <<- x
8+
inverse <<- NULL
9+
}
710

11+
get <- function() {
12+
input
13+
}
14+
15+
setInverse <- function(inv) {
16+
inverse <<- inv
17+
}
18+
19+
getInverse <- function() {
20+
inverse
21+
}
22+
23+
list(set = set, get = get,
24+
setInverse = setInverse,
25+
getInverse = getInverse)
826
}
927

1028

11-
## Write a short comment describing this function
29+
## Returns inverse of matrix 'x' from the cache if available
30+
## Otherwise computes and caches the inverse
1231

1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
inverse <- x$getInverse()
34+
if(!is.null(inverse)) {
35+
message("getting cached data")
36+
return(inverse)
37+
}
38+
39+
input <- x$get()
40+
inverse <- solve(input, ...)
41+
x$setInverse(inverse)
42+
inverse
1543
}

0 commit comments

Comments
 (0)