Skip to content

Instantly share code, notes, and snippets.

@themiguelamador
Created November 4, 2014 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themiguelamador/f7c99874c00e4b0e3083 to your computer and use it in GitHub Desktop.
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 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