|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Below are two functions that are used to create a special object that stores |
| 2 | +## a numeric matrix and caches its inversion |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
| 4 | +## The first function, makeCacheMatrix creates a special "vector" |
| 5 | +## which is really a list containing a function to |
5 | 6 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 |
| -##test |
8 |
| -} |
| 7 | +## 1. set the value of the matrix |
| 8 | +## 2. get the value of the matrix |
| 9 | +## 3. set the value of the inversion |
| 10 | +## 4. get the value of the inversion |
9 | 11 |
|
| 12 | +makeCacheMatrix <- function(x = matrix()) |
| 13 | +{ |
| 14 | + inv <- NULL |
| 15 | + set <- function(y) |
| 16 | + { |
| 17 | + x <<- y |
| 18 | + inv <<- NULL |
| 19 | + } |
| 20 | + get <- function() x |
| 21 | + setinv <- function(i) inv <<- i |
| 22 | + getinv <- function() inv |
| 23 | + list(set = set, get = get, setinv = setinv, getinv = getinv) |
| 24 | +} |
10 | 25 |
|
11 |
| -## Write a short comment describing this function |
| 26 | +## The following function calculates the inversion of the special "vector" |
| 27 | +## created with the makeCacheMatrix function. It first checks if the inversion |
| 28 | +## has already been calculated. If so, it gets the inversion from the cache and |
| 29 | +## skips the computation. Otherwise, it calculates the inversion of the data |
| 30 | +## and sets the value of the inversion in the cache via the setinv function. |
12 | 31 |
|
13 |
| -cacheSolve <- function(x, ...) { |
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 32 | +cacheSolve <- function(x, ...) |
| 33 | +{ |
| 34 | + inv <- x$getinv() |
| 35 | + if (!is.null(inv)) |
| 36 | + { |
| 37 | + message("getting cached data") |
| 38 | + return(inv) |
| 39 | + } |
| 40 | + data <- x$get() |
| 41 | + inv <- solve(data, ...) |
| 42 | + x$setinv(inv) |
| 43 | + inv |
15 | 44 | }
|
0 commit comments