forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR-assignment submission
60 lines (44 loc) · 1.36 KB
/
R-assignment submission
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Define the combined functions
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # Variable to store the inverse
# Set the value of the matrix
set <- function(y) {
x <<- y
inv <<- NULL # Reset the inverse when a new matrix is set
}
# Get the value of the matrix
get <- function() x
# Set the value of the inverse
setInverse <- function(inverse) inv <<- inverse
# Get the value of the inverse
getInverse <- function() inv
# Return a list of functions to interact with the matrix and its inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
# Retrieve the cached inverse
inv <- x$getInverse()
# If the inverse is already cached, return it
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Otherwise, calculate the inverse
data <- x$get() # Get the matrix
inv <- solve(data, ...) # Compute the inverse
# Cache the inverse
x$setInverse(inv)
# Return the inverse
inv
}
# Example usage:
# Create a special matrix object
myMatrix <- makeCacheMatrix(matrix(c(1, 2, 3, 4), 2, 2))
# Compute the inverse for the first time (not cached)
inverse <- cacheSolve(myMatrix)
print(inverse)
# Get the cached inverse without recomputing
inverse_cached <- cacheSolve(myMatrix)
print(inverse_cached)