From 0caaf5fdf0ffbdfe94ee623c49166c157d863d05 Mon Sep 17 00:00:00 2001 From: alexfding Date: Tue, 22 Apr 2014 17:13:58 -0500 Subject: [PATCH] contain cacheMatrix solution code --- cachematrix.R | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..30cc7c41427 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do +## two functions that cache the inverse of a matrix(assumed invertiable) -## Write a short comment describing this function +## makeCacheMatrix creates a special "matrix" object which cache its inverse makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setsolve <- function(solve) m <<- solve + getsolve <- function() m + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) + } -## Write a short comment describing this function +##cacheSolve calculates the inverse of the special "matrix" above, and it checks first whether the invese +##has been calculated. If so "gets" from cache, otherwise calculates and sets via setsolve function cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m + }