From fdc2ed2038f970058ead038093ae84083f4e996d Mon Sep 17 00:00:00 2001 From: deepakjaiswal09 Date: Mon, 31 Mar 2025 09:53:34 +0530 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3547f813b40 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,13 +3,20 @@ ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { - +makeCacheMatrix <- function() { +mat <- NULL +inv_cache <- NULL +setMatrix <- function(x) { +mat <<- x +inv_cache <<<- NULL +getInverse <- function() { +if (is.null(inv_cache)) { +inv_cache <<- solve(mat) } - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +inv_cache +list(setMatrix setMatrix, getInverse = getInverse) +} +cacheSolve <- function(x,...) { +inv_cache <- x$getInverse() +inv_cache }