From e17bcba667047c4e13d1425e72233659f55aa09a Mon Sep 17 00:00:00 2001 From: PDonepudi Date: Mon, 21 Apr 2014 11:50:43 -0700 Subject: [PATCH 1/3] Update cachematrix.R --- cachematrix.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..385a79282a4 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,31 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse , + getinverse = getinverse ) } + ## Write a short comment describing this function cacheSolve <- function(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 } From 2e4d40939fd182c449920ab857e4fe76cc31495c Mon Sep 17 00:00:00 2001 From: PDonepudi Date: Mon, 21 Apr 2014 13:01:23 -0700 Subject: [PATCH 2/3] Update cachematrix.R --- cachematrix.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 385a79282a4..4e8604588ac 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,9 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## makeCacheMatrix : This function returns a list of functions set / get / setinverse / getinverse for a matrix +## passed as the parameter. + makeCacheMatrix <- function(x = matrix()) { m <- NULL @@ -19,7 +21,13 @@ makeCacheMatrix <- function(x = matrix()) { -## Write a short comment describing this function +## cacheSolve : This function executes the functions created in makeCacheMatrix function. + +## if there is a matrix for m (that was already created during the initial run, it will be returned. +## if there is no inverse calculation performed on m and is NULl, then it performs the following - +## a) an inverse is calculated is performed +## b) save the results into m +## c) Returnm the m value cacheSolve <- function(x, ...) { m <- x$getinverse () From 1ef673bcb5207796a1258edb1e1b25275f7bd31f Mon Sep 17 00:00:00 2001 From: PDonepudi Date: Mon, 21 Apr 2014 13:44:36 -0700 Subject: [PATCH 3/3] Update cachematrix.R --- cachematrix.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 4e8604588ac..ee8f9ef3b74 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -23,10 +23,13 @@ makeCacheMatrix <- function(x = matrix()) { ## cacheSolve : This function executes the functions created in makeCacheMatrix function. -## if there is a matrix for m (that was already created during the initial run, it will be returned. +## cacheSolve function takes as the parameter the list that is created in "makeCacheMatrix" function. +## The function tried to retrieve m (which should be the inverse of a matrix). +## If m is a valid matrix, then it returns m and ends the function. ## if there is no inverse calculation performed on m and is NULl, then it performs the following - -## a) an inverse is calculated is performed -## b) save the results into m +## a) read the matrix information using the get function. +## b) calculate the inverse function using solve +## b) save the results of m into the cachedmemory using setinverse function. ## c) Returnm the m value cacheSolve <- function(x, ...) {