Skip to content

Commit 19f5563

Browse files
committed
Functions to add the ability to cache the inverse of a Matrix
1 parent 7f657dd commit 19f5563

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

cachematrix.R

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Set of functions to add the ability to cache the inverse of the Matrix and to
2+
## use the cached value while getting the inverse of a Matrix
53

4+
## Adds the ability to cache the inverse of the matrix passed in as a paramter to this## function
65
makeCacheMatrix <- function(x = matrix()) {
7-
6+
inverse <- NULL
7+
set <- function(y) {
8+
x <<- y
9+
inverse <<- NULL
10+
}
11+
get <- function() x
12+
setinverse <- function(i) inverse <<- i
13+
getinverse <- function() inverse
14+
list(get = get, set = set, getinverse = getinverse, setinverse = setinverse)
815
}
916

1017

11-
## Write a short comment describing this function
12-
18+
## Gets the inverse of the Matrix and caches the result if required.
1319
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
20+
## Return a matrix that is the inverse of 'x'
21+
i <- x$getinverse()
22+
if (! is.null(i)) {
23+
message("Getting cached inverse")
24+
return(i)
25+
}
26+
m <- x$get()
27+
i <- solve(m, ...)
28+
x$setinverse(i)
29+
i
1530
}

0 commit comments

Comments
 (0)