From 4d5a3df43383cc5173b6b67569ebd865e4346f6d Mon Sep 17 00:00:00 2001 From: Kwame Date: Fri, 13 Mar 2015 18:45:52 +0000 Subject: [PATCH 1/2] cache matrix completed --- cachematrix.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..07a5c856182 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,17 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + mi <- NULL + set <- function(y) { + x <<- y + mi <<- NULL + } + get <- function() x + setmatrix_inv <- function(solvex) mi <<- solvex + getmatrix_inv <- function() mi + list(set = set, get = get, + setmatrix_inv = setmatrix_inv, + getmatrix_inv = getmatrix_inv) } @@ -12,4 +22,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + mi <- x$getmatrix_inv() + if(!is.null(mi)) { + message("getting cached data") + return(mi) + } + data <- x$get() + mi <- solve(data, ...) + x$setmatrix_inv(mi) + mi } From b5375e377182bb6b4ca3eea540057a3a67c28fbe Mon Sep 17 00:00:00 2001 From: Kwame Date: Fri, 13 Mar 2015 19:00:11 +0000 Subject: [PATCH 2/2] updated comments --- cachematrix.R | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 07a5c856182..e01fa7be1a3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,8 +1,8 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## cacheSolve works objects created with makeCacheMatrix to +## store previously computed results of matrix inverses +## makeCacheMatrix sets up a matrix object via a list +## to store itself and a cache of its inverse makeCacheMatrix <- function(x = matrix()) { mi <- NULL set <- function(y) { @@ -17,9 +17,8 @@ makeCacheMatrix <- function(x = matrix()) { getmatrix_inv = getmatrix_inv) } - -## Write a short comment describing this function - +## cacheSolve first checks if the matrix's inverse has previously been +## computed and cached to be returned. Computes and cache the inverse otherwise. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' mi <- x$getmatrix_inv()