|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## makeCacheMatrix and solveCache can be used to cache |
| 2 | +## the matrix inverse computation. A matrix created by |
| 3 | +## makeCacheMatrix is a |
| 4 | +## special list containing functions to set and get |
| 5 | +## the matrix and to get and set the inverse as well. |
| 6 | +## solveCache will calculate the inverse and cache |
| 7 | +## the result of that computation. |
3 | 8 |
|
4 |
| -## Write a short comment describing this function |
5 | 9 |
|
| 10 | +## Create a matrix which allows caching the inverse |
| 11 | +## Return a list with four functions |
| 12 | +## 1. set the matrix |
| 13 | +## 2. get the matrix |
| 14 | +## 3. set the inverse of the matrix |
| 15 | +## 4. get the inverse of the matrix |
6 | 16 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 17 | + i <- NULL |
| 18 | + set <- function(y) { |
| 19 | + x <<- y |
| 20 | + i <<- NULL |
| 21 | + } |
| 22 | + get <- function() x |
| 23 | + setinverse <- function(inverse) i <<- inverse |
| 24 | + getinverse <- function() i |
| 25 | + list(set = set, |
| 26 | + get = get, |
| 27 | + setinverse = setinverse, |
| 28 | + getinverse = getinverse) |
8 | 29 | }
|
9 | 30 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 31 | +## cacheSolve returns the inverse of a special |
| 32 | +## matrix created with the makeCacheMatrix |
| 33 | +## results of the computation will be cached |
13 | 34 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 35 | + ## Return a matrix that is the inverse of 'x' |
| 36 | + |
| 37 | + i <- x$getinverse() |
| 38 | + if(!is.null(i)) { |
| 39 | + message("getting cached data") |
| 40 | + return(i) |
| 41 | + } |
| 42 | + data <- x$get() |
| 43 | + i <- solve(data, ...) |
| 44 | + x$setinverse(i) |
| 45 | + i |
15 | 46 | }
|
0 commit comments