From 421b1ec18bc5abc0feb3c5c2dcab825acd1c12a1 Mon Sep 17 00:00:00 2001 From: Edwin <63673069+StatiQQQ309@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:50:30 -0500 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..122a8096c1a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,45 @@ ## functions do ## Write a short comment describing this function - +## Create a special matrix object that can cache its inverse +## Returns four functions: set, get, setinv, getinv makeCacheMatrix <- function(x = matrix()) { - + # Stores the cached inverse + inv<- NULL + # Assigns and clears cached inverse + set<- function(y) { + x <<- y + inv <<- NULL + } + # Returns current matrix + get<-function() x + # Stores inversed matrix in cache + setinv<-function(inverse) inv<<- inverse + # Retrieves cached inverse + getinv<-function() inv + # Return list of the four functions created + list(set = set, get = get, + setinv=setinv, + getinv=getinv) } ## Write a short comment describing this function - +## Compute the inverse of the matrix created by makeCacheMatrix +## If already cached, return the cached value cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + # Gets cached inverse + inv<-x$getinv() + # IF cached inverse exists, return it + if (!is.null(inv)) { + return(inv) + } + # Else compute the inverse + mat<-x$get() + inv<-solve(mat, ...) + x$setinv(inv) + + inv }