From 95d6b3fb59776ca12f00d997580d3454a35d0107 Mon Sep 17 00:00:00 2001 From: bobl49 Date: Sun, 26 Apr 2015 21:36:57 -0400 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ceee24f54ae 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,44 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions make an object which is a cacheable form of +## a matrix and its inverse. To use it, first call +## invX<-makeCacheMatrix(x) +## invX$set(x) +## Then to retrieve the inverse, call +## cacheSolve(xInv) +## to see x call +## inv$get() -## Write a short comment describing this function +## makeCacheMatrix creates a cacheable form of the matrix x and +## its inverse m makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setInverse <- function(inverse) m <<- inverse + getInverse <- function() m + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function +## cacheSolve takes as arguments an object x +##and other ... arguments for the solve() functioncreated by the makeCacheMatrix +## function and returns its inverse, either from cache or computed +## directly. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setInverse(m) + m }