Created
November 4, 2014 21:49
-
-
Save themiguelamador/f7c99874c00e4b0e3083 to your computer and use it in GitHub Desktop.
This function implements a way of caching the result of the inverse matrix of a given matrix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This function implements a way of caching the result of the inverse matrix of a given matrix. | |
## makeCacheMatrix creates a special matrix from an initial matrix can can hold | |
## any additional information, wich can be used to store its inverse. | |
## Implementation of sub functions to set and set both objects. | |
makeCacheMatrix <- function(x = matrix()) { | |
m <- NULL | |
set <- function(y) { | |
x <<- y | |
m <<- NULL | |
} | |
get <- function() x | |
setinv <- function(inv) m <<- inv | |
getinv <- function() m | |
list(set = set, get = get, | |
setinv = setinv, | |
getinv = getinv) | |
} | |
## CacheSolve acts on a special "matrix" (x) to retreive its inverse, either using | |
## pre-existing stored value, or by calculating it, and caching it on the | |
## provided matrix (x). | |
cacheSolve <- function(x, ...) { | |
## Return a matrix that is the inverse of 'x' | |
m <- x$getinv() | |
if(!is.null(m)) { | |
message("getting cached data") | |
return(m) | |
} | |
data <- x$get() | |
m <- solve(data) | |
x$setinv(m) | |
m | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment