From 91808b63498082dda8892e835a6ffa98350adaf0 Mon Sep 17 00:00:00 2001 From: Arne Claassen Date: Sun, 27 Jul 2014 13:11:24 -0700 Subject: [PATCH] assignment solution --- cachematrix.R | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e3de09386a9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do +## This file contains two functions for creating a matrix container than can cache the inverse operation of said matrix +## and a function to perform this inversion on that container -## Write a short comment describing this function +## makeCacheMatrix optionally takes a matrix and constructs a container that +## allows the setting and getting of the the contained matrix as well as the +## inverse of that matrix makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## cacheSolve requires a cacheable matrix container and will either return +## the already contained cached version of the inverse of the contained +## matrix or compute the inverse, cache it in the container and then return it cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached inverse matrix") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i }