From 6c997409c4799529512d0401c8c4280ef44316a2 Mon Sep 17 00:00:00 2001 From: Nicole Casing Date: Thu, 24 Jul 2025 13:34:41 +0800 Subject: [PATCH 1/2] Assignment2 --- Assignment2 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Assignment2 diff --git a/Assignment2 b/Assignment2 new file mode 100644 index 00000000000..bf7e26cb60e --- /dev/null +++ b/Assignment2 @@ -0,0 +1,33 @@ +## "makeCacheMatrix" is a function that 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 + setsolve <- function(solve) inv <<- solve + getsolve <- function() inv + list(set = set, get = get. + setsolve = setsolve, + getsolve = getsolve) +} + + +## "cacheSolve" is a function that computes the matrix inversion of the special "matrix" +## returned by makeCacheMatrix above. If the matrix inversion has already been calculated, this function will retrieve the inverse of the matrix from the cache. +## If the matrix inversion has not been computed, this function calculates the matrix inversion +## and sets the value of the inverse in the cache via the setmean function + +cacheSolve <- function(x, ...) { + inv <- x$getsolve() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setsolve(inv) + inv +} From 8e5f322ffc159284d34b8351e8ac9986b3b996a4 Mon Sep 17 00:00:00 2001 From: Nicole Casing Date: Thu, 24 Jul 2025 13:47:08 +0800 Subject: [PATCH 2/2] Assignment2