From 1e84200729aa4972c0c0198b440ef7af17cf03ce Mon Sep 17 00:00:00 2001 From: mlnotes2718 <150231238+mlnotes2718@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:08:10 +0800 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ccd39b7983e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,13 +3,39 @@ ## Write a short comment describing this function +## This function will create a matrix function that allows user to set their matrix ($set), read the matrix($get) +## This function will also allow user to read the inverse of function ($getinv) +## If the result is NULL, then user need to run the cacheSolve() function makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(solve) m <<- solve + getinv <- function() m + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } - ## Write a short comment describing this function +## If makeCacheMatrix$getinv() is NULL, then user need to run the following function +## This function will compute the inverse of matrix and store the result to the function that was +## previously created in makeCachematric() + + cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinv() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinv(m) + m }