|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## These two functions work very similarly to how the example functions for this programming assignment |
| 2 | +## (that is, makeVector and cacheMean) work |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
| 4 | +## makeCacheMatrix creates a special "matrix" which is really a list containing functions to: |
| 5 | +## 1- set the value of the matrix |
| 6 | +## 2- get the value of the matrix |
| 7 | +## 3- set the value of the inverse |
| 8 | +## 4- get the value of the inverse |
5 | 9 |
|
6 | 10 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 11 | + i <- NULL |
| 12 | + set <- function(y) { |
| 13 | + x <<- y |
| 14 | + i <<- NULL |
| 15 | + } |
| 16 | + get <- function() x |
| 17 | + setinverse <- function(inv) i<<- inv |
| 18 | + getinverse <- function() i |
| 19 | + list(set= set, get = get, setinverse= setinverse, getinverse= getinverse) |
8 | 20 | }
|
9 | 21 |
|
10 | 22 |
|
11 |
| -## Write a short comment describing this function |
| 23 | +## cacheSolve calculates the inverse of a special "matrix" created with the above function. |
| 24 | +## It first checks to see if the inverse has already been calculated, and if so, returns the |
| 25 | +## inverse value from the cache and skips the computation. Otherwise, it calculates the inverse |
| 26 | +## of the matrix - using solve(x)- and sets the value of the cached inverse via the setinverse function. |
12 | 27 |
|
13 | 28 | cacheSolve <- function(x, ...) {
|
14 | 29 | ## Return a matrix that is the inverse of 'x'
|
| 30 | + i <- x$getinverse() |
| 31 | + if (!is.null(i)) { |
| 32 | + message("getting cached data") |
| 33 | + return(i) |
| 34 | + } |
| 35 | + data <- x$get() |
| 36 | + i <- solve(data) |
| 37 | + x$setinverse(i) |
| 38 | + i |
15 | 39 | }
|
0 commit comments