|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Two functions that are used to create a special object |
| 2 | +## that stores a matrix and cache's its inverse |
| 3 | +## |
3 | 4 |
|
4 |
| -## Write a short comment describing this function |
| 5 | +## Function: makeCacheMatrix |
| 6 | +## Description: This function creates a special "matrix" object |
| 7 | +## that can cache it inverse |
5 | 8 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 9 | +makeCacheMatrix <- function(matrix = matrix()) { |
| 10 | + #invMatrix - inverse matrix value |
| 11 | + invMatrix <- NULL #default value - NULL |
7 | 12 |
|
| 13 | + #setMatrix function: matrix and reset invMatrix |
| 14 | + setMatrix <- function(y){ |
| 15 | + matrix <<- y |
| 16 | + invMatrix <<- NULL |
| 17 | + } |
| 18 | + #getMatrix function: get matrix |
| 19 | + getMatrix <- function() matrix |
| 20 | + |
| 21 | + #setInvMatrix function: set inverse matrix |
| 22 | + setInvMatrix <- function(inverseMatrix) invMatrix <<- inverseMatrix |
| 23 | + |
| 24 | + #getInvMatrix function: get inverse matrix |
| 25 | + getInvMatrix <- function() invMatrix |
| 26 | + |
| 27 | + list(setMatrix = setMatrix, getMatrix=getMatrix, |
| 28 | + setInvMatrix = setInvMatrix, |
| 29 | + getInvMatrix = getInvMatrix) |
8 | 30 | }
|
9 | 31 |
|
10 | 32 |
|
11 |
| -## Write a short comment describing this function |
| 33 | +## Function: cacheSolve |
| 34 | +## Description: This function computes the inverse of the |
| 35 | +## special "matrix" returned by makeCacheMatrix |
| 36 | + |
| 37 | +cacheSolve <- function(matrix, ...) { |
| 38 | + #get invMatrix |
| 39 | + invMatrix <- matrix$getInvMatrix() |
| 40 | + |
| 41 | + #check if it is not null |
| 42 | + if (!is.null(invMatrix)){ |
| 43 | + message("getting cached data") |
| 44 | + return (invMatrix) |
| 45 | + } |
12 | 46 |
|
13 |
| -cacheSolve <- function(x, ...) { |
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 47 | + #if it is null, calculate the inverse |
| 48 | + data <- matrix$getMatrix() |
| 49 | + inv <- solve(data, ...) |
| 50 | + matrix$setInvMatrix(inv) #save it |
| 51 | + inv |
15 | 52 | }
|
0 commit comments