|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Assignment for Week 3. Looking at Lexical Scoping |
| 2 | +## Objective is to write 2 functions that create a matrix and store it, then inverse |
| 3 | +## the matrix, but checking for its precense of a cached matrix first, before creating one. |
3 | 4 |
|
4 |
| -## Write a short comment describing this function |
5 |
| - |
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 5 | +## The first function will create an invertible matrix, get the inverse and store the inverse for later use. |
7 | 6 |
|
| 7 | +makeCacheMatrix <- function(x = matrix()) { ## setting the argument with mode of matrix |
| 8 | + inv <- NULL ## establishing variable to hold the inverse of the matrix |
| 9 | + set <- function(y){ ## giving the matrix in the parent environment a value |
| 10 | + x <<- y |
| 11 | + inv <<- NULL |
| 12 | + } |
| 13 | + get <- function() x ## returning the matrix value |
| 14 | + setInverse <- function(solveMatrix) inv <<- solveMatrix ## establishing the inverse value |
| 15 | + getInverse <- function() inv ## calls or gets the inverse value |
| 16 | + list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) ## making the functions available to be called later |
8 | 17 | }
|
9 | 18 |
|
10 | 19 |
|
11 |
| -## Write a short comment describing this function |
| 20 | +## determining the inverse of the matrix created in the MakeCacheMatrix function. |
| 21 | +## If the inverse is in the cache, then use that value, otherwise get the inverse. |
12 | 22 |
|
13 | 23 | cacheSolve <- function(x, ...) {
|
14 | 24 | ## Return a matrix that is the inverse of 'x'
|
| 25 | + inv <- x$getInverse() ## populate the inv variable with the value from the previous function,. |
| 26 | + if(!is.null(inv)){ ## if the variable is not null then retrieve the cache value. |
| 27 | + message("getting cached data") |
| 28 | + return(inv) |
| 29 | + } |
| 30 | + data <- x$get() ## If the inv variable is null, generate the inverse and set to inv variable. |
| 31 | + inv <- solve(data) |
| 32 | + x$setInverse(inv) |
| 33 | + inv |
15 | 34 | }
|
0 commit comments