|
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 | +## At last, you can cache your inverted matrices, like a squirrel hiding delicious numerical acorns! |
| 2 | +## * Use makeCacheMatrix(matrix) to create a cache object for your matrix |
| 3 | +## * Pass that object to cacheSolve(cacheMatrix) to check the cache for the inverse, if it exists, |
| 4 | +## or calculate the inverse and cache it away for the future |
5 | 5 |
|
| 6 | +## makeCacheMatrix takes your numerical matrix and then returns a special caching matrix with functions to: |
| 7 | +# - set the value of your matrix |
| 8 | +# - get the value of your matrix |
| 9 | +# - set the inverse of your matrix |
| 10 | +# - get the inverse of your matrix |
| 11 | +## This function does not actually calculate the inverse of your matrix |
6 | 12 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 13 | + m <- NULL |
| 14 | + set <- function(y) { |
| 15 | + x <<- y |
| 16 | + m <<- NULL |
| 17 | + } |
| 18 | + get <- function() x |
| 19 | + setinverse <- function(inverseMatrix) m <<- inverseMatrix |
| 20 | + getinverse <- function() m |
| 21 | + list(set = set, get = get, |
| 22 | + setinverse = setinverse, |
| 23 | + getinverse = getinverse) |
8 | 24 | }
|
9 | 25 |
|
10 | 26 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 27 | +## cacheSolve takes your cacheMatrix, created above, and will check to see if the inverse is already cached |
| 28 | +## - if it's cached, cacheSolve warns "getting cached data" and returns the cache |
| 29 | +## - if it's not cached, cacheSolve calculates the inverse of your original matrix, caches it, |
| 30 | +## and returns the value |
13 | 31 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 32 | + m <- x$getinverse() |
| 33 | + if(!is.null(m)) { |
| 34 | + message("getting cached data") |
| 35 | + return(m) |
| 36 | + } |
| 37 | + data <- x$get() |
| 38 | + m <-solve(data, ...) |
| 39 | + x$setinverse(m) |
| 40 | + m |
15 | 41 | }
|
| 42 | + |
0 commit comments