diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..47fe4b6c62a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,32 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## Create an object to store a matrix and its' inverse +makeCacheMatrix <- function(m = matrix()) { + i <- NULL + set <- function(y) { + m <<- y + inv <<- NULL + } + get <- function() m + setinv <- function(inv) i <<- inv + getinv <- function() i + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## Chave the inverse of a matrix using an objetc +## returned by makeCacheMatrix -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(m, ...) { + i <- m$getinv() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- m$get() + i <- solve(data, ...) + m$setinv(i) + i }