|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Matrix inversion is usually a costly computation and |
| 2 | +## there may be some benefit to caching the inverse of a matrix |
| 3 | +## rather than compute it repeatedly. |
| 4 | +## |
| 5 | +## The functions below cache the inverse of a matrix. |
3 | 6 |
|
4 |
| -## Write a short comment describing this function |
| 7 | +# AUTHOR: Fabio Correa |
| 8 | + |
| 9 | +# COURSE: R Programming (rprog-032) |
| 10 | +# TASK : Programming Assignment 2 - Caching the Inverse of a Matrix |
5 | 11 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 12 | +## Create special matrix object that caches its inverse. |
7 | 13 |
|
| 14 | +makeCacheMatrix <- function(x = matrix()) { |
| 15 | + x.inverse <- NULL |
| 16 | + set <- function(x.new) { |
| 17 | + x <<- x.new |
| 18 | + x.inverse <<- NULL |
| 19 | + } |
| 20 | + get <- function() x |
| 21 | + setinverse <- function(inv) x.inverse <<- inv |
| 22 | + getinverse <- function() x.inverse |
| 23 | + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) |
8 | 24 | }
|
9 | 25 |
|
10 | 26 |
|
11 |
| -## Write a short comment describing this function |
| 27 | +## Compute the inverse of the special matrix returned by |
| 28 | +## 'makeCacheMatrix', unless it exists in cache, where it |
| 29 | +## is retrieved from. |
12 | 30 |
|
13 | 31 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 32 | + ## Return a matrix that is the inverse of 'x' |
| 33 | + x.inverse <- x$getinverse() |
| 34 | + if(!is.null(x.inverse)) { |
| 35 | + message("Getting cached data") |
| 36 | + return(x.inverse) |
| 37 | + } |
| 38 | + data <- x$get() |
| 39 | + x.inverse <- solve(data, ...) |
| 40 | + x$setinverse(x.inverse) |
| 41 | + x.inverse |
15 | 42 | }
|
0 commit comments