diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e01fa7be1a3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## cacheSolve works objects created with makeCacheMatrix to +## store previously computed results of matrix inverses +## makeCacheMatrix sets up a matrix object via a list +## to store itself and a cache of its inverse makeCacheMatrix <- function(x = matrix()) { - + mi <- NULL + set <- function(y) { + x <<- y + mi <<- NULL + } + get <- function() x + setmatrix_inv <- function(solvex) mi <<- solvex + getmatrix_inv <- function() mi + list(set = set, get = get, + setmatrix_inv = setmatrix_inv, + getmatrix_inv = getmatrix_inv) } - -## Write a short comment describing this function - +## cacheSolve first checks if the matrix's inverse has previously been +## computed and cached to be returned. Computes and cache the inverse otherwise. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + mi <- x$getmatrix_inv() + if(!is.null(mi)) { + message("getting cached data") + return(mi) + } + data <- x$get() + mi <- solve(data, ...) + x$setmatrix_inv(mi) + mi }