From 1eb0eb2725b7e3f2b608c30263d0f88ff462ac43 Mon Sep 17 00:00:00 2001 From: mitashri16tikli-byte Date: Wed, 28 Jan 2026 17:59:38 +0530 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4e2eed1d1a2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,27 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix is a function that can cashe the inverse of a matrix. +## If the inverse has already been calculated (and the matrix has not changed), then the cacheSolve function retrieves the inverse from the cache. -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + get <- function() x + getinv <- function() inv <<- solve(x) + list(get = get, + getinv = getinv) } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + inv }