diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..0d84b89944a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix() creates "matrix" object and +## returns list of functions to manipulate it. +## +## cacheSolve() returns the inverse of a matrix. +## It first checks if the inverse of a matrix has already been calculated, +## otherwise it calculates the inverse of a matrix and puts it to the cache +## +## Example: +## a <- matrix(c(1,2,3,4), nrow=2, ncol=2) +## m <- makeCacheMatrix(a) +## cacheSolve(m) -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## Creates "matrix" object +makeCacheMatrix <- function(x = matrix()) { + x_inverse <- NULL + set <- function(y) { + x <<- y + x_inverse <<- NULL + } + get <- function() x + setinverse <- function(inv) x_inverse <<- inv + getinverse <- function() x_inverse + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function - +## Calculates the inverse of a matrix object or +## gets it from the cache cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data) + x$setinverse(inv) + inv }