From d476bb7dc02237733377cc39be87d6729c0a6e18 Mon Sep 17 00:00:00 2001 From: Hann <92386727+Amatasuchi@users.noreply.github.com> Date: Fri, 26 Jul 2024 03:19:30 -0400 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d1895f8f37c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,34 @@ ## functions do ## Write a short comment describing this function +# This function wiil cache the inverse matrix, setinverse() will cache the +# inverse and getinverse() will return the cached matrix makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function () x + setinverse <- function(inv) inv_matrix <<- inv + getinverse <- function() inv_matrix + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } -## Write a short comment describing this function +## Write a short comment describing this function\ +# The function will get the inverse matrix from x, checks if the cache already +# exist, if not it will get the matrix and solve using solve() function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if (!is.null(m)) { + message("Getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m }