From b22d55dcf3fb17cf03344806ceb61836c098151f Mon Sep 17 00:00:00 2001 From: Patrick Ashamalla Date: Thu, 23 Apr 2015 21:37:41 -0400 Subject: [PATCH 1/2] added makeCacheMatrix and cacheSolve --- .gitignore | 4 ++++ cachematrix.R | 32 ++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..1c079f289fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +ProgrammingAssignment2.Rproj diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..337df5ed15d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do +## The following functions allow you to create a matrix and cache the inverse of a +## matrix for later reference if it is available -## Write a short comment describing this function +## makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function( y ) { + x <<- y + inv <<- NULL + } + get <- function() x + setinverse <- function(inverse) inv <<- inverse + getinverse <- function() inv + list( set = set, get = get, setinverse = setinverse, getinverse = getinverse ) } -## Write a short comment describing this function +## cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix +## above. If the inverse has already been calculated (and the matrix has not changed), +## then the cachesolve should retrieve the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + message('still null') + data <- x$get() + inv <- solve(data) + x$setinverse(inv) + inv } From d0deacd3f3a3b99e4cd06b346af50ca0d48ecfc3 Mon Sep 17 00:00:00 2001 From: Patrick Ashamalla Date: Thu, 23 Apr 2015 21:38:31 -0400 Subject: [PATCH 2/2] Removed ## still null --- cachematrix.R | 1 - 1 file changed, 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 337df5ed15d..4f19d66fffd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -27,7 +27,6 @@ cacheSolve <- function(x, ...) { message("getting cached data") return(inv) } - message('still null') data <- x$get() inv <- solve(data) x$setinverse(inv)