From ad84df1779301ce1734524f893b84b438baa82d4 Mon Sep 17 00:00:00 2001 From: Christian Fielitz Date: Thu, 16 Jul 2015 20:44:48 -0400 Subject: [PATCH 1/2] Assignment --- cachematrix.R | 79 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6c9cdd6990d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,64 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +## Put comments here that give an overall description of what your +## functions do + +# + +## Write a short comment describing this function + +makeCacheMatrix <- function(x = matrix()) { + + #Description: This function creates a special "matrix" object + #that can cache its inverse + + #Input: x as a square, invertible matrix (Input from cacheSolve) + #1. set the value of the matrix + #2. get the value of the matrix + #3. set the value of the inverse + #4. get the value of the inverse + #Output: + + inverse_i <- NULL + + set <- function(y) { + x <<- y + inverse_i <<- NULL + } + + get <- function() x + setinverse <- function(inverse) inverse_i <<- inverse + getinverse <- function() inverse_i + + 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' + + #Description: Return a matrix that is the inverse of 'x' + #Input: x = output from function makeCacheMatrix() + #Output: Inverse of x => input for makeCacheMatrix() + + #Check if the inverse of x has already been cached/ calculated + inverse_i=x$getinverse() + + if(!is.null(inverse_i)) { + message("getting cached data...") + return(inverse_i) + } + + #In case the inverse of x has not been calculated/ cached yet, calculate the inverse + inverse_data.data = x$get() + inverse_i=solve(inverse_data.data, ...) + + #Set and return the inverse + x$setinverse(inverse_i) + + return (inverse_i) +} From 20c2994b3ded96de5862eae33fa1709343d59206 Mon Sep 17 00:00:00 2001 From: Christian Fielitz Date: Thu, 16 Jul 2015 20:56:58 -0400 Subject: [PATCH 2/2] Update 2 --- cachematrix.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6c9cdd6990d..7190c96e7ee 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -7,7 +7,7 @@ makeCacheMatrix <- function(x = matrix()) { - #Description: This function creates a special "matrix" object + #Description/ Output: This function creates a special "matrix" object #that can cache its inverse #Input: x as a square, invertible matrix (Input from cacheSolve) @@ -15,10 +15,12 @@ makeCacheMatrix <- function(x = matrix()) { #2. get the value of the matrix #3. set the value of the inverse #4. get the value of the inverse - #Output: + + #Initiatve variable inverse_i <- NULL + #1-4 set <- function(y) { x <<- y inverse_i <<- NULL