|
1 | 1 | ## Put comments here that give an overall description of what your
|
2 | 2 | ## functions do
|
3 |
| - |
4 | 3 | ## Write a short comment describing this function
|
5 |
| - |
| 4 | +## This creates a cachematrix object where x is defined as a matrix |
6 | 5 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 6 | + ## NULL is assigned the variable m |
| 7 | + m <- NULL |
| 8 | + ## Sets the value of the matrix |
| 9 | + set <- function(y) { |
| 10 | + ## y is superassigned x which is defined as a matrix |
| 11 | + x <<- y |
| 12 | + ## NULL is superassigned to m |
| 13 | + m <<- NULL |
| 14 | + } |
| 15 | + ## Get the value of the matrix depending on the supplied vector |
| 16 | + get <- function() x |
| 17 | + ## Sets the value and gets the value of the inverse |
| 18 | + setinverse <- function(solve) m <<- solve |
| 19 | + getinverse <- function() m |
| 20 | + list(set = set, get = get, |
| 21 | + setinverse = setinverse, |
| 22 | + getinverse = getinverse) |
8 | 23 | }
|
9 | 24 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 25 | +## cacheSolve function takes the arguments x, in this case a matrix,and ... |
13 | 26 | cacheSolve <- function(x, ...) {
|
14 | 27 | ## Return a matrix that is the inverse of 'x'
|
| 28 | + ## gets the inverse of the matrix and assigns it to m |
| 29 | + m <- x$getinverse() |
| 30 | + |
| 31 | + ## if m is NOT a null value, previously solved, then print message |
| 32 | + if(!is.null(m)) { |
| 33 | + message("getting cached data") |
| 34 | + return(m) |
| 35 | + } |
| 36 | + ## otherwise assign the get function a name 'data' |
| 37 | + data <- x$get() |
| 38 | + ## and passes in 'data' as an argument in the solve function assigned to m |
| 39 | + m <- solve(data, ...) |
| 40 | + ## ...which sets the inverse of the cached matrix |
| 41 | + x$setinverse(m) |
| 42 | + m |
15 | 43 | }
|
| 44 | + |
0 commit comments