From 4dc5701a98ab3a2767c806e42f0444b5f446c2f0 Mon Sep 17 00:00:00 2001 From: Mashcode Date: Tue, 16 Dec 2014 15:48:03 -0500 Subject: [PATCH 1/2] programming assignment initial commit --- cachematrix.R | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..40a0caaca20 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,44 @@ ## Put comments here that give an overall description of what your ## functions do - ## Write a short comment describing this function - +## This creates a cachematrix object where x is defined as a matrix makeCacheMatrix <- function(x = matrix()) { - + ## NULL is assigned the variable m + m <- NULL + ## Sets the value of the matrix + set <- function(y) { + ## y is superassigned x which is defined as a matrix + x <<- y + ## NULL is superassigned to m + m <<- NULL + } + ## Get the value of the matrix depending on the supplied vector + get <- function() x + ## Sets the value and gets the value of the inverse + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function - +## cacheSolve function takes the arguments x, in this case a matrix,and ... cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + ## gets the inverse of the matrix and assigns it to m + m <- x$getinverse() + + ## if m is NOT a null value, previously solved, then print message + if(!is.null(m)) { + message("getting cached data") + return(m) + } + ## otherwise assign the get function a name 'data' + data <- x$get() + ## and passes in 'data' as an argument in the solve function assigned to m + m <- solve(data, ...) + ## ...which sets the inverse of the cached matrix + x$setinverse(m) + m } + From 9087fb098ec5819088619fb94763daac3a70b447 Mon Sep 17 00:00:00 2001 From: Mashcode Date: Sun, 21 Dec 2014 13:58:45 -0500 Subject: [PATCH 2/2] programming assignment updated comments --- cachematrix.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 40a0caaca20..3f2a72cd6b6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,11 +1,10 @@ -## Put comments here that give an overall description of what your -## functions do +## Put comments here that give an overall description of what your functions do ## Write a short comment describing this function -## This creates a cachematrix object where x is defined as a matrix +## This function ncreates a CacheMatrix object where x is defined as a matrix makeCacheMatrix <- function(x = matrix()) { ## NULL is assigned the variable m m <- NULL - ## Sets the value of the matrix + ## Set the value of the matrix set <- function(y) { ## y is superassigned x which is defined as a matrix x <<- y @@ -16,26 +15,29 @@ makeCacheMatrix <- function(x = matrix()) { get <- function() x ## Sets the value and gets the value of the inverse setinverse <- function(solve) m <<- solve + ## The following is code I can't comment on intelligently + ## A list of elements of getinverse? getinverse <- function() m list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } -## cacheSolve function takes the arguments x, in this case a matrix,and ... +## The cacheSolve function takes the arguments x, in this case a matrix,and ... cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## ...returns a matrix that is the inverse of 'x' ## gets the inverse of the matrix and assigns it to m m <- x$getinverse() - ## if m is NOT a null value, previously solved, then print message + ## if solution is previously cached print message + ## else return null if(!is.null(m)) { message("getting cached data") return(m) } ## otherwise assign the get function a name 'data' data <- x$get() - ## and passes in 'data' as an argument in the solve function assigned to m + ## and pass in 'data' as an argument in the solve function assigned to m m <- solve(data, ...) ## ...which sets the inverse of the cached matrix x$setinverse(m)