From edb5be20a0d61c3bcc06b1e10fd330c39a264926 Mon Sep 17 00:00:00 2001 From: "Matias I. Cosimano" Date: Sat, 21 Feb 2026 11:37:05 -0300 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ace521011c0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -13,3 +13,43 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' } + + +## My solution (Matias Ignacio Cosimano) +## makeCacheMatrix - creates a "matrix" object that can cache its inverse + +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) +} + +## cacheSolve - returns the inverse of the matrix stored in the object + +cacheSolve <- function(x, ...) { + inv <- x$getInverse() + + if (!is.null(inv)) { + message("getting cached inverse") + return(inv) + } + + mat <- x$get() + inv <- solve(mat, ...) + x$setInverse(inv) + inv +}