From ca50649d93b48bb7381d1899214f3e7a6818fba5 Mon Sep 17 00:00:00 2001 From: brobie Date: Thu, 22 May 2014 16:40:41 -0500 Subject: [PATCH 1/2] completed assignment - before testing. --- .gitignore | 3 +++ ProgrammingAssignment2.Rproj | 13 +++++++++++++ cachematrix.R | 33 +++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 ProgrammingAssignment2.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..066341ea144 --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..29c9f4f7145 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions provide a way to create an inverted matrix, and everytime +## thereafter return a cached version of the matrix until another +## matrix is inverted. -## Write a short comment describing this function +## This function provides access to set and get a cached version of a matrix. makeCacheMatrix <- function(x = matrix()) { - + im <- NULL + set <- function(y) { + x <<- y + im <<- NULL + } + get <- function() x + setsolve <- function(inMatrix) im <<- inMatrix + getsolve <- function() im + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function +## This function returns an inverse of the matrix given to the makeCacheMatrix +## function. If the matrix has already been inverted and cached, it will return +## the cached version, otherwise, it will invert and then cache the matrix. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached matrix") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m } From dff219a451d99b0cc467cdd845c68d517df6b177 Mon Sep 17 00:00:00 2001 From: brobie Date: Fri, 23 May 2014 13:34:49 -0500 Subject: [PATCH 2/2] Better commenting --- cachematrix.R | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 29c9f4f7145..2aa25d6637f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,9 +1,15 @@ ## These functions provide a way to create an inverted matrix, and everytime ## thereafter return a cached version of the matrix until another -## matrix is inverted. +## matrix is set. -## This function provides access to set and get a cached version of a matrix. +############################################################################# +## makeCacheMatrix: This function provides access to set and get a cached +## version of a matrix. +## x | matrix | The matrix to be set + +## return list containing different functions used to get/set matrix +############################################################################# makeCacheMatrix <- function(x = matrix()) { im <- NULL set <- function(y) { @@ -18,11 +24,15 @@ makeCacheMatrix <- function(x = matrix()) { getsolve = getsolve) } - -## This function returns an inverse of the matrix given to the makeCacheMatrix +############################################################################# +## cacheSolve: This function returns an inverse of the matrix given to the makeCacheMatrix ## function. If the matrix has already been inverted and cached, it will return ## the cached version, otherwise, it will invert and then cache the matrix. +## x | list | The list of functions used to get/set matrix + +## return inverted matrix +############################################################################# cacheSolve <- function(x, ...) { m <- x$getsolve() if(!is.null(m)) {