From 6bc669960f7d6a610e6d71e51cf7833c2cff6d7c Mon Sep 17 00:00:00 2001 From: hobbiton Date: Wed, 2 Sep 2020 21:13:00 -0400 Subject: [PATCH] edit for asg 002 coursera --- cachematrix.R | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..91c0ded0129 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,6 +5,21 @@ makeCacheMatrix <- function(x = matrix()) { + ainv <- NULL + + set <- function(newMatrix) + { + x <<- newMatrix + ainv <<- NULL + } + + get <- function() x + setInv <- function(inv) ainv <<- inv + getInv <- function() ainv + + list (set=set,get=get,setInv=setInv,getInv=getInv) + + } @@ -12,4 +27,15 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + if (is.null(x$getInv())) + { + message("calculating Inverse ::") + inv <- solve(x$get(),...) + x$setInv(inv) + } + else + message("Getting Inv from cache:") + x$getInv() + }