|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## These functions work together to either solve for the inverse of a |
| 2 | +## matrix or to retrieve the inverse if it is already stored in memory. |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
| 4 | +## makeCacheMatrix creates a four step vector of functions for each scenario |
| 5 | +## that cacheSolve requires to store and retrieve the inverse from the cache. |
5 | 6 |
|
6 | 7 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 8 | + m <- NULL |
| 9 | + |
| 10 | + ## Set Matrix in Cache to Null |
| 11 | + set <- function(y) { |
| 12 | + x <<- y |
| 13 | + m <<- NULL |
| 14 | + } |
| 15 | + |
| 16 | + ## Get Matrix |
| 17 | + get <- function() x |
| 18 | + |
| 19 | + ## Set Inverse in Cache |
| 20 | + setinverse <- function(inverse) m <<- inverse |
| 21 | + |
| 22 | + ## Get Inverse from Cache |
| 23 | + getinverse <- function() m |
| 24 | + |
| 25 | + ## Return list of functions |
| 26 | + list(set = set, get = get, |
| 27 | + setinverse = setinverse, |
| 28 | + getinverse = getinverse) |
8 | 29 | }
|
9 | 30 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
| 31 | +## The cacheSolve function calculates the inverse of a given matrix or |
| 32 | +## retrieves the existing result from cache. |
12 | 33 |
|
13 | 34 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 35 | + ## If m is not null return the cache data |
| 36 | + m <- x$getinverse() |
| 37 | + if(!is.null(m)) { |
| 38 | + message("getting cached data") |
| 39 | + return(m) |
| 40 | + } |
| 41 | + |
| 42 | + ## Otherwise calculate the matrix inverse |
| 43 | + data <- x$get() |
| 44 | + m <- solve(data, ...) |
| 45 | + |
| 46 | + ## And then store the results in the cache |
| 47 | + x$setinverse(m) |
| 48 | + |
| 49 | + ## Return the newly calculate inverse of the matrix |
| 50 | + m |
15 | 51 | }
|
0 commit comments