|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## The following functions are used in tandom to create a matrix along with calculating and |
| 2 | +## caching its inverse (a time consuming operation). |
| 3 | +## |
| 4 | +## Example usage |
| 5 | +## a<-makeCacheMatrix() |
| 6 | +## a$set(matrix(1:4,2,2)) |
| 7 | +## cacheSolve(a) |
3 | 8 |
|
4 |
| -## Write a short comment describing this function |
| 9 | +## makeCacheMatrix takes a matrix as an argument, calls cacheSolve() to calculate and |
| 10 | +## store the inverse of the matrix in cache. The method also contains functions to get the matrix and |
| 11 | +## to get and set the inverse of the matrix. |
5 | 12 |
|
6 | 13 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 14 | + inverseMatrix <- NULL |
| 15 | + set <- function(y) { |
| 16 | + x <<- y |
| 17 | + inverseMatrix <<- NULL |
| 18 | + } |
| 19 | + get <- function() x |
| 20 | + setInverseMatrix <- function(solve) inverseMatrix <<- solve |
| 21 | + getInverseMatrix <- function() inverseMatrix |
| 22 | + list(set = set, get = get, |
| 23 | + setInverseMatrix = setInverseMatrix, |
| 24 | + getInverseMatrix = getInverseMatrix) |
8 | 25 | }
|
9 | 26 |
|
10 | 27 |
|
11 |
| -## Write a short comment describing this function |
| 28 | +## cacheSolve takes in a matrix as an argument and returns the cached inverse if it exists, |
| 29 | +## otherwise it calculates the inverse, stores it in cache and then returns it. |
12 | 30 |
|
13 |
| -cacheSolve <- function(x, ...) { |
| 31 | +cacheSolve <- function(x =, ...) { |
14 | 32 | ## Return a matrix that is the inverse of 'x'
|
| 33 | + |
| 34 | + inverseMatrix <- x$getInverseMatrix() |
| 35 | + if(!is.null(inverseMatrix)) { |
| 36 | + message("getting cached data") |
| 37 | + return(inverseMatrix) |
| 38 | + } |
| 39 | + data <- x$get() |
| 40 | + inverseMatrix <- solve(data, ...) |
| 41 | + x$setInverseMatrix(inverseMatrix) |
| 42 | + inverseMatrix |
15 | 43 | }
|
0 commit comments