From 1e7b11ba74ad30e43fde9590bc855c5ff0dedcbe Mon Sep 17 00:00:00 2001 From: AndyH Date: Fri, 19 Dec 2014 12:51:39 -0800 Subject: [PATCH] adding methods to solve hw2 --- cachematrix.R | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..73cbacf5a43 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,18 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + invert_x <- NULL + + set <- function(y) { + x <<- y + invert_x <<- NULL + } + + get <- function() x + setinverted <- function(inverted) invert_x <<- inverted + getinverted <- function() invert_x + + list(set = set, get = get,setinverted = setinverted,getinverted = getinverted) } @@ -12,4 +23,15 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inverted <- x$getinverted() + + if(!is.null(inverted)) { + message("Getting Cached Data") + return(inverted) + } + norm <- x$get() + inverted <- solve(norm) + x$setinverted(inverted) + + inverted }