|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
3 |
| - |
4 |
| -## Write a short comment describing this function |
5 |
| - |
| 1 | +## This function creates a special "matrix" object that can cache its inverse |
| 2 | +## This special matrix has an internal cache "i" to do this. |
| 3 | +## There are three "methods" in this matrix: |
| 4 | +## set(value) to set the value |
| 5 | +## get() to retrive the value |
| 6 | +## setinverse(value) to fill the cache |
| 7 | +## getinverse() to get data from cache |
6 | 8 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 9 | + i <- NULL |
| 10 | + set <- function(y) { |
| 11 | + x <<- y |
| 12 | + i <<- NULL |
| 13 | + } |
| 14 | + get <- function() x |
| 15 | + setinverse<- function(i) i <<-i |
| 16 | + getinverse <- function() i |
| 17 | + list(set = set, get = get, |
| 18 | + setinverse = setinverse, |
| 19 | + getinverse = getinverse) |
8 | 20 | }
|
9 | 21 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 22 | +## This function computes the inverse of the special "matrix" |
| 23 | +## returned by makeCacheMatrix above. If the inverse has already been |
| 24 | +## calculated (and the matrix has not changed), then cacheSolve retrieves |
| 25 | +## the inverse from the cache. |
| 26 | +## Otherwise the inverse function (solve) is used to invert the matrix. |
13 | 27 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 28 | + i <- x$getinverse() |
| 29 | + if (!is.null(i)) { |
| 30 | + message("found cached inverse matrix") |
| 31 | + return(i) |
| 32 | + } else { |
| 33 | + i <- solve(x$get()) |
| 34 | + x$setinverse(i) |
| 35 | + return(i) |
| 36 | + } |
15 | 37 | }
|
0 commit comments