From 9c6f5709a357266afa8b7b9bf9de1af23c1b83fe Mon Sep 17 00:00:00 2001 From: smartgamer Date: Mon, 28 Nov 2016 23:53:00 -0500 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..9b15a6209a1 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,34 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +#makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. +#cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +#If the inverse has already been calculated (and the matrix has not changed), then cacheSolve should retrieve the inverse from the cache. +## Write a cache function makeCacheMatrix <- function(x = matrix()) { - + inverse <- NULL + set <- function(y) { + x <<- y + inverse <<- NULL + } + get <- function() x + setsolve <- function(solve) inverse <<- solve + getsolve <- function() inverse + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function - +## get an inverse matrix from cache if there's one, or make a new one cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + x.inverse <- x$getsolve() + if(!is.null(x.inverse)) { + message("getting cached data") + return(x.inverse) + } + data <- x$get() + x.inverse <- solve(data, ...) + x$setsolve(x.inverse) + x.inverse + }