From be279a66ed2c1fd8f7c79336b997d56f234cdf62 Mon Sep 17 00:00:00 2001 From: jamesmcm Date: Wed, 10 Dec 2014 11:09:59 +0100 Subject: [PATCH] Upload --- cachematrix.R | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..5a524f1e65e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions allow one to calculate and cache a matrix inverse, +## avoiding the need to recalculate the inverse if it is needed multiple times. -## Write a short comment describing this function +### Example: +### > source("./cachematrix.R") +### > mymat <- makeCacheMatrix(matrix(c(1,2,3,3,2,1,4,5,2),ncol=3,nrow=3)) +### > cacheSolve(mymat) +### [,1] [,2] [,3] +### [1,] -0.0625 -0.125 0.4375 +### [2,] 0.6875 -0.625 0.1875 +### [3,] -0.2500 0.500 -0.2500 +### +### > cacheSolve(mymat) +### getting cached data +### [,1] [,2] [,3] +### [1,] -0.0625 -0.125 0.4375 +### [2,] 0.6875 -0.625 0.1875 +### [3,] -0.2500 0.500 -0.2500 + +## This creates the matrix object that will store the cached inverse, and a method to calculate the inverse makeCacheMatrix <- function(x = matrix()) { + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinv <- function(inv) i <<- inv + getinv <- function() i + return( list(set = set, get = get, + setinv = setinv, + getinv = getinv) ) } -## Write a short comment describing this function - +## This function will return the inverse of a matrix, via the cached value if possible cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + + i <- x$getinv() + if(!is.null(i)) { + message("getting cached data") + return(i) #if we already cached inverse then return and stop here + } + #otherwise calculate and cache inverse + data <- x$get() + i <- solve(data, ...) #solve for inverse - R function + x$setinv(i) #cache inverse + return(i) }