From efaf5377018240b6275d3e20ad3cce9688574ec8 Mon Sep 17 00:00:00 2001 From: Cam Linke Date: Sun, 27 Apr 2014 12:53:46 -0600 Subject: [PATCH 1/3] added comments explaining function --- cachematrix.R | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..68fbf3cae27 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,22 @@ ## Put comments here that give an overall description of what your ## functions do +## This library is creates a matrix that can cache it's inverse +## and gives computes the inverse of the created matrix + ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. +makeCacheMatrix <- function(x = matrix()) { + } ## Write a short comment describing this function +## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should +## retrieve the inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From bb8f2e052334a75a360e1a0f4c1f510eb15bf594 Mon Sep 17 00:00:00 2001 From: Cam Linke Date: Sun, 27 Apr 2014 12:58:44 -0600 Subject: [PATCH 2/3] first pass at solution --- cachematrix.R | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 68fbf3cae27..7209751b4fd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -9,6 +9,14 @@ ## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(matrix) m <<- mean + getmatrix <- function() m } @@ -19,5 +27,13 @@ makeCacheMatrix <- function(x = matrix()) { ## retrieve the inverse from the cache. 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 <- matrix(data, ...) + x$setmatrix(m) + m } From 669cabcfb9a49004d32c6a66b9ba9839a50f426e Mon Sep 17 00:00:00 2001 From: Cam Linke Date: Thu, 19 Jun 2014 08:40:08 -0600 Subject: [PATCH 3/3] created cachematrix.r implementation --- cachematrix.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 7209751b4fd..51f1a7298e7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -15,8 +15,12 @@ makeCacheMatrix <- function(x = matrix()) { m <<- NULL } get <- function() x - setmatrix <- function(matrix) m <<- mean - getmatrix <- function() m + ## Set and cache the inverse of the matrix using the solve function + setinverse <- function(solve) m <<- inverse + getinverse <- function() m + ## Create a list containing a function to set the value of the matrix, get the value of the matrix, + ## set the value of the inverse, and get the value of the inverse. + list(set=set, get=get, setinverse=setinverse, getinverse=getinverse) } @@ -27,13 +31,16 @@ makeCacheMatrix <- function(x = matrix()) { ## retrieve the inverse from the cache. cacheSolve <- function(x, ...) { - m <- x$getmatrix() + m <- x$getinverse() + ## check to see if there is already a cached inverse if(!is.null(m)) { message("getting cached data") return(m) } + + ## If there is no cached inverse calculate the inverse, cache it and return it data <- x$get() - m <- matrix(data, ...) - x$setmatrix(m) + m <- solve(data, ...) + x$setinverse(m) m }