From ea379db25a0507a505021870990e521a4741e7e4 Mon Sep 17 00:00:00 2001 From: srikg20 <126743544+srikg20@users.noreply.github.com> Date: Thu, 5 Jun 2025 18:22:40 +0530 Subject: [PATCH 1/2] Update cachematrix.R --- cachematrix.R | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..19ebce471a1 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its median. +## It includes functions to set and get the matrix, and to set and get the cached median. -makeCacheMatrix <- function(x = matrix()) { +## This function computes the median of the special "matrix" returned by makeCacheMatrix. + +makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmedian <- function(median) m <<- median + getmedian <- function() m + list(set = set, get = get, + setmedian = setmedian, + getmedian = getmedian) + } -## Write a short comment describing this function -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + +## If the median has already been calculated (and the matrix has not changed), +## then it retrieves the median from the cache. + + + + cacheMedian <- function(x, ...) { + m <- x$getmedian() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- median(data, ...) + x$setmedian(m) + m } + + From 627a01d717aa820664b97426c6fcfe4828ca99c5 Mon Sep 17 00:00:00 2001 From: srikg20 <126743544+srikg20@users.noreply.github.com> Date: Mon, 16 Jun 2025 15:27:30 +0530 Subject: [PATCH 2/2] Update cachematrix.R --- cachematrix.R | 60 +++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 19ebce471a1..d6a76149f3a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -6,38 +6,42 @@ ## This function computes the median of the special "matrix" returned by makeCacheMatrix. makeCacheMatrix <- function(x = matrix()) { - m <- NULL - set <- function(y) { - x <<- y - m <<- NULL - } - get <- function() x - setmedian <- function(median) m <<- median - getmedian <- function() m - list(set = set, get = get, - setmedian = setmedian, - getmedian = getmedian) - + inv <- NULL + + set <- function(y) { + x <<- y + inv <<- NULL # Reset the inverse when the matrix is changed + } + + get <- function() x + + setInverse <- function(inverse) inv <<- inverse + + getInverse <- function() inv + + list(set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse) } -## If the median has already been calculated (and the matrix has not changed), -## then it retrieves the median from the cache. - - - - cacheMedian <- function(x, ...) { - m <- x$getmedian() - if(!is.null(m)) { - message("getting cached data") - return(m) - } - data <- x$get() - m <- median(data, ...) - x$setmedian(m) - m -} +cacheSolve <- function(x, ...) { +  # Try to get the cached inverse +  inv <- x$getInverse() +  +  # If the inverse is already cached, return it +  if (!is.null(inv)) { +    message("Getting cached inverse") +    return(inv) +  } +  +  # If not cached, compute the inverse +  mat <- x$get()         # Get the matrix +  inv <- solve(mat, ...) # Compute the inverse using solve() +  +  # Cache the inverse for future use