From 8890a1f8e914bc38dc1ffb56fe3ba0a1970f1a46 Mon Sep 17 00:00:00 2001 From: manjiribabar2002-afk Date: Fri, 17 Apr 2026 10:44:25 -0400 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..f6e5fc93e10 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,30 @@ ## Write a short comment describing this function 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 <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getInverse() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setInverse(inv) + inv }