From c1797702be103e0803869a43aef8bbbf7d4a6a92 Mon Sep 17 00:00:00 2001 From: rubyist Date: Sat, 25 Apr 2015 10:31:04 -0400 Subject: [PATCH] caching matrix --- cachematrix.R | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..1e85db72d2f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,32 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions provide a method of caching matrix inversions -## Write a short comment describing this function +## makeCacheMatrix creates an object wrapping a matrix that allows the calculated +## inverse to be cached. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + + get <- function() x + setsolve <- function(inv) i <<- inv + getsolve <- function() i + list(set = set, get = get, setsolve = setsolve, getsolve = getsolve) } -## Write a short comment describing this function +## cacheSolve will invert a matrix created by makeCacheMatrix and cache the results, +## returning a matrix that is the inverse of 'x'. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getsolve() + if (!is.null(i)) { + return(i) + } + m <- x$get() + i <- solve(m, ...) + x$setsolve(i) + i }