diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4200e5e1afb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Functions to calculate the inverse of a matrix +## using cacheing to avoid repeat calulations +## +## Example usage: +## v <- matrix(c(1,2,3,4),ncol=2) +## mat <- makeCacheMatrix() +## mat$set(v) +## cacheSolve(mat) +## Returns a hold the input and cached value +## to be used by the 'cacheSolve' function 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 - +## Takes a list created by the 'makeCacheMatrix' function +## and returns the inverse (using a cached value if available) 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) + } else { + message("calculating new value") + } + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m +} \ No newline at end of file