diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..973eb8a761a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Following two functions are used to create a special object that +## stores a matrix and cache's its inverse matrix. +## +## The first function, makeCacheMatrix creates a special "vector", +## which is really a list containing a function to +## set the value of the vector +## get the value of the vector +## set the value of the inverse +## get the value of the inverse 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 calculates the inverse of the special "vector" created +## with the above function. However, it first checks to see if the inverse has +## already been calculated. If so, it gets the inverse from the cache and skips +## the computation. Otherwise, it calculates the inverse of the data and sets +## the value of the inverse in the cache via the setinverse function. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)){ + message("getting cached data") + return (m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m }