From 81c53e18ddeb1a81df710b2f98f0cd0c13975d8c Mon Sep 17 00:00:00 2001 From: Alexander McMurray Date: Sun, 18 Jan 2015 18:25:26 +0000 Subject: [PATCH 1/2] Submitting solution --- cachematrix.R | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..5c948fc4913 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - makeCacheMatrix <- function(x = matrix()) { - + #This function creates a special "matrix" object that can cache its inverse. + inverse<-NULL + set <- function(y){ + x<<-y + inverse<<-NULL + } + get <- function() x + setinverse <- function(inv) inverse<<-inv + getinverse <- function() inverse + list(set=set, get=get, setinverse=setinverse, getinverse=getinverse) } -## Write a short comment describing this function cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + # 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. + inverse<-x$getinverse() + if(!is.null(inverse)){ + message("getting cached data") + return(inverse) + } + data<-x$get() + inverse<-solve(data, ...) + #ellipses in case we want to pass additional arguments to solve() + #note the ellipses in the function def that implies this functionality + #is desired + x$setinverse(inverse) #set it for caching + inverse #return } From ff93c1bd1abccf04cd931a2dbec1d2331ca43a99 Mon Sep 17 00:00:00 2001 From: Alexander McMurray Date: Sun, 18 Jan 2015 18:32:48 +0000 Subject: [PATCH 2/2] Submitting solution2 --- cachematrix.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 5c948fc4913..5f43cf077e7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,16 @@ makeCacheMatrix <- function(x = matrix()) { #This function creates a special "matrix" object that can cache its inverse. - inverse<-NULL + inverse<-NULL set <- function(y){ x<<-y - inverse<<-NULL + inverse<<-NULL + #this means if we change the matrix, we won't retain the old, now incorrect inverse } get <- function() x setinverse <- function(inv) inverse<<-inv getinverse <- function() inverse - list(set=set, get=get, setinverse=setinverse, getinverse=getinverse) + #return list of functions + list(set=set, get=get, setinverse=setinverse, getinverse=getinverse) }