diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6711c65f871 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,46 @@ ## Put comments here that give an overall description of what your ## functions do +## Matrix inversion is usually a costly computation and there may be some benefit +## to caching the inverse of a matrix rather than compute it repeatedly. The +## following two functions are used to cache the inverse of a matrix. -## Write a short comment describing this function +## makeCacheMatrix creates a list containing a function to +## 1. set the value of the matrix +## 2. get the value of the matrix +## 3. set the value of inverse of the matrix +## 4. get the value of inverse of the matrix makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) m <<- inverse + getinverse <- function() m + list(set=set, get=get, + setinverse=setinverse, + getinverse=getinverse) } -## Write a short comment describing this function +# The following function returns the inverse of the matrix. It first checks if +# the inverse has already been computed. If so, it gets the result and skips the +# computation. If not, it computes the inverse, sets the value in the cache via +# setinverse function. + +# This function assumes that the matrix is always invertible. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting from cached data.") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m }