diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..47c9cd5ccaf 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## makeCacheMatrix creates a structure to store the matrix, compute the inverse, +## and retrieve the cached inverse. makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinv <- function(solve) inv <<- solve + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## cacheSolve takes the output object of makeCacheMatrix as an input. If the inverse +## has already been cached, it will be returned (with a message). If not, it will compute, +## cache and return it. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' -} + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv +} \ No newline at end of file