|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## These functions will compute the inverse of a given matrix |
| 2 | +## This is assuming the matrix is inversible |
| 3 | +## Once the inverse has been calculated it is stored in a cache |
| 4 | +## If the inverse is requested again, the cached value is returned |
3 | 5 |
|
4 |
| -## Write a short comment describing this function |
5 | 6 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 | 7 |
|
| 8 | +## This function will create a special list containing functions |
| 9 | +## These functions can be used to set and get the matrix used |
| 10 | +## or to set and get the inverse of the matrix |
| 11 | + |
| 12 | +makeCacheMatrix <- function(x = matrix()) { |
| 13 | + i <- NULL |
| 14 | + set <- function(y) { |
| 15 | + x <<- y |
| 16 | + i <<- NULL |
| 17 | + } |
| 18 | + get <- function() x |
| 19 | + setinverse <- function(inverse) i <<- inverse |
| 20 | + getinverse <- function() i |
| 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 |
| 27 | +## This function uses the special list created by 'makeCacheMatrix' |
| 28 | +## It will check for a cached value of the inverse matrix and return it if found |
| 29 | +## If no cached value is found, calculate, set and return the inverse matrix |
12 | 30 |
|
13 | 31 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 32 | + i <- x$getinverse() |
| 33 | + if(!is.null(i)) { |
| 34 | + message("getting cached data") |
| 35 | + return(i) |
| 36 | + } |
| 37 | + data <- x$get() |
| 38 | + i <- solve(data, ...) |
| 39 | + x$setinverse(i) |
| 40 | + i |
15 | 41 | }
|
0 commit comments