From 33ca4e1b575ca151f3c3e93983c9c4822830d5a9 Mon Sep 17 00:00:00 2001 From: mishahmadian Date: Wed, 30 Mar 2016 15:42:33 -0500 Subject: [PATCH 1/2] commit cachematrix --- cachematrix.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a0b65edf98b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,8 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + #this is a test + #test } From 2c6f36a53975b62950d480673c784165f7feb14e Mon Sep 17 00:00:00 2001 From: mishahmadian Date: Wed, 30 Mar 2016 16:35:55 -0500 Subject: [PATCH 2/2] commit final cachematrix --- cachematrix.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a0b65edf98b..48bb13819ce 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## Matrix inversion is usually a costly computation and there +## may be some benefit to caching the inverse of a matrix rather +## than compute it repeatedly. -## Write a short comment describing this function +## makeCacheMatrix: This function creates a special "matrix" +## object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - #this is a test - #test + rev <- NULL + set <- function(y) { + x <<- y + rev <<- NULL + } + get <- function() x + setRev <- function(revers) rev <<- revers + getRev <- function() rev + list(set = set, get = get, setRev = setRev, getRev = getRev) } -## Write a short comment describing this function +## 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 the cachesolve should retrieve the inverse +## from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + rev <- x$getRev() + if(!is.null(rev)) { + message("Getting from cached data") + return(rev) + } + dat <- x$get() + rev <- solve(dat, ...) + x$setRev(rev) + rev }