From 73a7ba3edd79df066f2825fc7c1daaa926c8eb9a Mon Sep 17 00:00:00 2001 From: drbsantos Date: Thu, 14 May 2026 15:21:05 +0100 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..863aa7b01db 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,39 @@ ## functions do ## Write a short comment describing this function + ##The first function, `makeCacheMatrix` creates a special "Matrix", object that can cache its inverse. -makeCacheMatrix <- function(x = matrix()) { +makeCacheMatrix <- function(x = matrix()) { + inv<- NULL + set <- function(y){ + x <<- y + inv <<- NULL + } + + get<- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set= set, get=get, + setinv = setinv, + getinv = getinv) } ## Write a short comment describing this function + ##This second function computes the inverse of the special "matrix" (obtained bu solve function) returned by `makeCacheMatrix` above. + ##If the inverse has already been calculated (and the matrix has not changed), then`cacheSolve` should + ##retrieve the inverse from the cache. 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, ...) + x$setinv(inv) + inv }