diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ae851e289f5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do +## Contains a pair of functions to cache an inverse matrix. The value +## from makeCacheMatrix should be the first parameter to cacheSolve. -## Write a short comment describing this function +## Creates a matrix structure whose inverse matrix can be cached. +## The resulting list has the following structure: +## - set: sets the original matrix to be solved. +## - get: gets the original matrix. +## - setinverse: sets the inverse matrix of the original matrix. +## - getinverse: gets the inverse matrix of the original matrix. makeCacheMatrix <- function(x = matrix()) { - + solved <- NULL + set <- function(y) { + x <<- y + solved <<- NULL + } + get <- function() x + setinverse <- function(inverse) solved <<- inverse + getinverse <- function() solved + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## Returns the inverse of a matrix if it has been solved. Otherwise, it +## will solve the inverse matrix and cache it for future use. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i }