diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4cb38ef4bbc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This lets you to cache `solve` operation for your matrix +## Accepts matrix, returns list of functions makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(matrix) m <<- matrix + getmatrix <- function() m + list(set = set, get = get, + setmatrix = setmatrix, + getmatrix = getmatrix) } - -## Write a short comment describing this function - +## Accepts list of functions, and uses them to store result of `solve` +## operation into cache or get it from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getmatrix() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setmatrix(m) + m } +