From d46544d529dd15149270f37d036a8b11a7ddeae1 Mon Sep 17 00:00:00 2001 From: androidJohn Date: Thu, 17 Apr 2014 08:14:46 -0700 Subject: [PATCH 1/2] Added full functions for the programming assignment. --- cachematrix.R | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..367ebef371b 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 two functions create a matrix and then the inverse of the matrix. +## The inverse is cached and if the same matrix is inverted it will read +## the cached version. ## Write a short comment describing this function - + # This function supplies setter and getter methods for the + # original matrix and the inverse makeCacheMatrix <- function(x = matrix()) { - + matrix <- NULL + setMatrix <- function(y) { + x <<- y + matrix <<- NULL + } + getMatrix <- function() x + setMatrixInverse <- function(cacheMatrix) matrix <<- cacheMatrix + getMatrixInverse <- function() matrix + list(setMatrix = setMatrix, getMatrix = getMatrix, + setMatrixInverse = setMatrixInverse, getMatrixInverse = getMatrixInverse) } ## Write a short comment describing this function - + # This function checks to see if there is already a matrix stored + # in the variable matrix then returns the cached version if it exists + # or creates one if it does not. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + matrix <- x$getMatrixInverse() + if(!is.null(matrix)) { + message("getting cached data") + return(matrix) + } + data <- x$getMatrix() + cacheMatrix <- solve(data, ...) + x$setMatrixInverse(cacheMatrix) + cacheMatrix } From 68f6a27dae36ccecd4a0ef9ff1fad61284bf2e4f Mon Sep 17 00:00:00 2001 From: evanswjohn Date: Sun, 15 Feb 2015 12:35:27 -0800 Subject: [PATCH 2/2] Assignment 2 cachematrix.R working --- cachematrix.R | 1 - makeVector.R | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 makeVector.R diff --git a/cachematrix.R b/cachematrix.R index 367ebef371b..d7402eb1f87 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -18,7 +18,6 @@ makeCacheMatrix <- function(x = matrix()) { setMatrixInverse = setMatrixInverse, getMatrixInverse = getMatrixInverse) } - ## Write a short comment describing this function # This function checks to see if there is already a matrix stored # in the variable matrix then returns the cached version if it exists diff --git a/makeVector.R b/makeVector.R new file mode 100644 index 00000000000..66ad7d247e6 --- /dev/null +++ b/makeVector.R @@ -0,0 +1,23 @@ +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, setmean = setmean, getmean = getmean) +} + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} \ No newline at end of file