From 24ff8d60481dd0b1705af6086a9c846158aec6db Mon Sep 17 00:00:00 2001 From: LorenaD90 Date: Mon, 25 Aug 2025 11:31:22 +0200 Subject: [PATCH 1/2] Create .Rhistory cachematrix.R Co-Authored-By: Roger D. Peng <9612+rdpeng@users.noreply.github.com> --- .Rhistory | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .Rhistory diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..5c68edcd1fe --- /dev/null +++ b/.Rhistory @@ -0,0 +1,2 @@ +source("C:/Users/User/OneDrive/Desktop/R assignments/cachematrix.R") +source("C:/Users/User/OneDrive/Desktop/R assignments/cachematrix.R") From 3d518163d20e9163cb748d608e5a703656d1460b Mon Sep 17 00:00:00 2001 From: LorenaD90 Date: Mon, 25 Aug 2025 14:57:44 +0200 Subject: [PATCH 2/2] Update cachematrix.R --- cachematrix.R | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..fbd509c2b7f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -13,3 +13,46 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' } +## makeCacheMatrix creates a special "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 computes the inverse of the special "matrix" +## 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, ...) { + inv <- x$getInverse() + + # If inverse is already cached, return it + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + + # Otherwise, compute inverse, cache it, and return it + mat <- x$get() + inv <- solve(mat, ...) + x$setInverse(inv) + inv +}