diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..65fc96c7f06 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,29 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + mi = NULL + + getMatrix <- function() x + setMatrix <- function(matrix) { + mi <<- NULL; + x <<- matrix + } + + getCachedInverse <- function() mi + setCachedInverse <- function(inv) mi <<- inv + list(getMatrix = getMatrix, setMatrix = setMatrix, getCachedInverse = getCachedInverse, setCachedInverse = setCachedInverse) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + cached <- x$getCachedInverse() + if (!is.null(cached)) { + message("Guys, we have a cache hit here! What a miracle!") + return(cached) + } + inv <- solve(x$getMatrix(), ...) + x$setCachedInverse(inv) + inv }