|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## These methods can be used to cache the inverse calculation. |
| 2 | +## to create a cachable matrix, do this: |
| 3 | +## cachedMatrix <- makeCacheMatrix(matrix(1:4,2,2)) |
| 4 | +## to calculate the inverse do this: |
| 5 | +## cacheSolve(cachedMatrix) |
3 | 6 |
|
4 |
| -## Write a short comment describing this function |
5 | 7 |
|
| 8 | +## This function generates an Object that holds a matrix and it's |
| 9 | +## possibily cached inverse. In order to calculate the inverse using caching |
| 10 | +## use the cacheSolve method. |
6 | 11 | makeCacheMatrix <- function(x = matrix()) {
|
| 12 | + inverse <- NULL |
| 13 | + set <- function(y) { |
| 14 | + x <<- y |
| 15 | + inverse <<- NULL |
| 16 | + } |
| 17 | + get <- function() x |
| 18 | + setinverse <- function(inv) inverse <<- inv |
| 19 | + getinverse <- function() inverse |
| 20 | + list(set = set, get = get, |
| 21 | + setinverse = setinverse, |
| 22 | + getinverse = getinverse) |
7 | 23 |
|
8 | 24 | }
|
9 | 25 |
|
10 | 26 |
|
11 |
| -## Write a short comment describing this function |
| 27 | +## This function calculates the inverse of a CacheMatrix |
| 28 | +## which was constructed my using the makeCacheMatrix function. |
| 29 | +## The First call calculates the inverse, every succeeding call |
| 30 | +## uses the cached inverse. |
12 | 31 |
|
13 | 32 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 33 | + i <- x$getinverse() |
| 34 | + if(!is.null(i)) { |
| 35 | + message("getting cached data") |
| 36 | + return(i) |
| 37 | + } |
| 38 | + data <- x$get() |
| 39 | + i <- solve(data, ...) |
| 40 | + x$setinverse(i) |
| 41 | + i |
15 | 42 | }
|
0 commit comments