|
2 | 2 | ## functions do
|
3 | 3 |
|
4 | 4 | ## Write a short comment describing this function
|
| 5 | +# makeCacheMatix is a function that creates a list containing |
| 6 | +# functions that: |
| 7 | +# 1. sets the value of an invertible matrix. Note that it is |
| 8 | +# assumed that the matrix is invertible. |
| 9 | +# 2. gets the value of the invertible matrix. |
| 10 | +# 3. sets the value of the inverse of the matrix. |
| 11 | +# 4. gets the value of the inverse of the matrix. |
5 | 12 |
|
6 | 13 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 14 | + m <- NULL |
| 15 | + set <- function(y) { |
| 16 | + x <<- y |
| 17 | + m <<- NULL |
| 18 | + } |
| 19 | + get <- function() x |
| 20 | + set.inverse <- function(solve) m <<- solve |
| 21 | + get.inverse <- function() m |
| 22 | + list(set = set, get = get, |
| 23 | + set.inverse = set.inverse, |
| 24 | + get.inverse = get.inverse) |
8 | 25 | }
|
9 | 26 |
|
10 |
| - |
11 | 27 | ## Write a short comment describing this function
|
| 28 | +# This function calculates the inverse of the special matrix |
| 29 | +# that was created with 'makeCacheMatrix. If the inverse |
| 30 | +# has already been calculated, then it gets the cached value |
| 31 | +# of the inverse and reports this along with a message. If the |
| 32 | +# inverse has not been calculated, then it is calculated with |
| 33 | +# the function solve() |
12 | 34 |
|
13 | 35 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 36 | + ## Return a matrix that is the inverse of 'x' |
| 37 | + m <- x$get.inverse() |
| 38 | + if(!is.null(m)) { |
| 39 | + message("getting cached data") |
| 40 | + return(m) |
| 41 | + } |
| 42 | + data <- x$get() |
| 43 | + m <- solve(data, ...) |
| 44 | + x$set.inverse(m) |
| 45 | + m |
15 | 46 | }
|
0 commit comments