diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ef0b4ae92dd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,15 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL ## initialize inv as NULL; will hold value of inverse matrix + set <- function(y) { ## set to assign new function + x <<- y ## value of matrix in parent environment + inv <<- NULL ## if there is a new matrix, reset inv to NULL + } + get <- function() {x} ## define the get function - returns value of the matrix argument + setInverse <- function(inverse) {inv <<- inverse} ## assigns value of inv in parent environment + getInverse <- function() {inv} ## gets the value of inv where called + list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) } @@ -12,4 +20,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inv <- x$getInverse() + if(!is.null(inv)) { ## if inverse has not been already cached, it is cached and collected + message("cached data collected") + return(inv) + } + m <- x$get() + inv <- solve(m, ...) + x$setInverse(inv) + inv }