From ecb9c3bf7c68500b71607128870157095ea47ab5 Mon Sep 17 00:00:00 2001 From: Almas Zareen Date: Sat, 17 Jan 2026 18:33:11 +0000 Subject: [PATCH] Assingment Complete --- cachematrix.R | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..946cfe4fc70 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do +## This function creates a special matrix object that can cache its inverse ## 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 inverse") + return(inv) + } + + mat <- x$get() + inv <- solve(mat, ...) + x$setinverse(inv) + inv } + + ## Return a matrix that is the inverse of 'x' + +