From c68e61a538031ceeffa1b587a210baeb23d53a0b Mon Sep 17 00:00:00 2001 From: statsmith Date: Fri, 25 Apr 2014 17:18:33 -0400 Subject: [PATCH] Initial Commit to Programming 2 Assignment --- cachematrix.R | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..73cca1567bb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,42 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions used in conjunction with each other can +## be used to save processing time. Instead of repeatedly +## calculating the inverse of a matrix, it is stored in a +## retrievable cache. The cache changes only if the matrix +## called changes... -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## The makeCacheMatrix function is a constructor function +## that creates functions to set and get the matrix and +## the inverse of the matrix... +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 +## The cacheSolve function calls the functions created in the makeCacheMatrix to +## either retrieve or save the inverse of a matrix. If the matrix has not changed +## and the inverse has already been calculated, it retrieves the matrix inverse from +## the cache. Otherwise, it calculates the matrix inverse and stores it in the cache. 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) + } + data <- x$get() + m <- solve(data) + x$setInverse(m) + m } +