From dbadcf6fd994bc6f4b09fb69f81c2497e6e5ff4b Mon Sep 17 00:00:00 2001 From: Peter Georgeson Date: Sun, 17 May 2015 00:47:52 +1000 Subject: [PATCH] added cacheable matrix --- cachematrix.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..04c8c78d351 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## enable creation of a matrix like object with the ability to cache the inversion function +## usage: +## - m <- makeCacheMatrix( matrix( 1:4, nrow=2, ncol=2 ) ) +## - cacheSolve( m ) +## create a matrix that can be passed to cacheSolve, which solves and caches the inversion function makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setsolve <- function(solve) m <<- solve + getsolve <- function() m + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function - +## accepts as a parameter the object created by makeCacheMatrix +## solves the matrix and caches the result; returns this result if called again cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m }