From e2fe0c58cb76de4e2f5eea6f96a80f69a63a31d9 Mon Sep 17 00:00:00 2001 From: Dustin Brewer Date: Sat, 21 Nov 2015 22:30:16 -0500 Subject: [PATCH] Submit invered matrix assignment --- cachematrix.R | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a259639aefd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do +## Functions to cache a matrix, invert it and cache the result. +## Saves the inversion from being recomputed repeatedly. -## Write a short comment describing this function +## Function to get and set matrix to be inverted +## and cache the inverted result. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinversematrix <- function(inv) i <<- inv + getinversematrix <- function() i + list(set = set, get = get, + setinversematrix = setinversematrix, + getinversematrix = getinversematrix) } -## Write a short comment describing this function +## Function which determines whether or not to +## compute a new inverted matrix or read an +## existing on from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + i <- x$getinversematrix() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinversematrix(i) + i +} \ No newline at end of file