From 3710bb01175321c87e5e94f55ad8bc13fd9e947c Mon Sep 17 00:00:00 2001 From: asamasoma Date: Wed, 18 Jun 2014 21:05:35 -0400 Subject: [PATCH] Implements makeCacheMatrix() and cacheSolve() --- .gitignore | 3 +++ ProgrammingAssignment2.Rproj | 13 +++++++++++++ cachematrix.R | 30 ++++++++++++++++++++++++------ 3 files changed, 40 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..8e3c2ebc99e --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a3c2d420d18 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions store a matrix object and caches its inverse. -## Write a short comment describing this function +## This function provides getters and setters for a cachable matrix object. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(matrix) m <<- matrix + getmatrix <- function() m + list(set = set, get = get, + setmatrix = setmatrix, + getmatrix = getmatrix) } -## Write a short comment describing this function +## This calculates and caches the inverse of a matrix created with the above function. +## If called again with the same matrix, it retrieves the cached inverse instead. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getmatrix() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setmatrix(m) + m }