From cb6b1d47a271314d50cafbd8c9c3e9d79b76a2c6 Mon Sep 17 00:00:00 2001 From: JakeT Date: Wed, 11 Jun 2014 00:02:47 -0500 Subject: [PATCH 1/2] maybe I got it? --- cachematrix.R | 55 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..90d1499bfb2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,56 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +## stores a matrix and its inverse makeCacheMatrix <- function(x = matrix()) { - + + #initialize variables + inv <- NULL + + + #create 4 functions: + #1. set matrix - stores the matrix and wipes out the cache + setmatrix <- function (y) { + inv <<- NULL + x <<- y + } + + #2. get matrix - returns the stored matrix (so you can assign the matrix, plus functions elsewhere) + getmatrix <- function() x + + #3. set inv - sets the inverse cache to whatever you tell it + setinv <- function (inverseval) { + inv <<- inverseval + } + + #4. get inv - just returns the cached inv + getinv <- function () inv + + #wrap the 4 functions up into a list + list(setmatrix= setmatrix, + getmatrix= getmatrix, + setinv = setinv, + getinv = getinv) + } ## Write a short comment describing this function + +## Returns a matrix that is the inverse of 'x' cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + #test if x$getinv is null + if(is.null(x$getinv()) ) { + #if so, get the matrix itself... + matr <- x$getmatrix() + # then solve for the inverse + inv <- solve(matr) + + #if it's not null + } else { + #just grab the cached inverse + inv <- x$getinv() + } + + return(inv) + } From 2d9120dde084c1124f8c58f5acb9011c9561aba4 Mon Sep 17 00:00:00 2001 From: Jake Tolbert Date: Mon, 23 Jun 2014 12:29:00 -0500 Subject: [PATCH 2/2] removed default comments --- cachematrix.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 90d1499bfb2..40956cd6690 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -32,8 +32,6 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function - ## Returns a matrix that is the inverse of 'x' cacheSolve <- function(x, ...) {