|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## The below two functions caches, computes the |
| 2 | +## inverse of a matrix. |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
5 |
| - |
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 4 | +## makeCacheMatrix creates a matrix object |
| 5 | +## that can cache its inverse. |
7 | 6 |
|
| 7 | +makeCacheMatrix <- function(mtx = matrix()) { |
| 8 | + inverse <- NULL |
| 9 | + set <- function(x) { |
| 10 | + mtx <<- x; |
| 11 | + inverse <<- NULL; |
| 12 | + } |
| 13 | + get <- function() return(mtx); |
| 14 | + setinv <- function(inv) inverse <<- inv; |
| 15 | + getinv <- function() return(inverse); |
| 16 | + return(list(set = set, get = get, setinv = setinv, getinv = getinv)) |
8 | 17 | }
|
9 | 18 |
|
10 | 19 |
|
11 |
| -## Write a short comment describing this function |
| 20 | +## cacheSolve function computes the inverse of the |
| 21 | +## matrix returned by makeCacheMatrix. If the inverse has |
| 22 | +## already been calculated (and the matrix has not changed), then |
| 23 | +## cacheSolve should retrieve the inverse from the cache. |
12 | 24 |
|
13 |
| -cacheSolve <- function(x, ...) { |
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 25 | +cacheSolve <- function(mtx, ...) { |
| 26 | + inverse <- mtx$getinv() |
| 27 | + if(!is.null(inverse)) { |
| 28 | + message("Retrieving cached data") |
| 29 | + return(inverse) |
| 30 | + } |
| 31 | + data <- mtx$get() |
| 32 | + invserse <- solve(data, ...) |
| 33 | + mtx$setinv(inverse) |
| 34 | + return(inverse) |
15 | 35 | }
|
0 commit comments