-
Notifications
You must be signed in to change notification settings - Fork 144k
Expand file tree
/
Copy pathHuiYi
More file actions
37 lines (30 loc) · 874 Bytes
/
HuiYi
File metadata and controls
37 lines (30 loc) · 874 Bytes
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
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # Initialize the inverse as NULL
# Function to set the matrix and reset cached inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
# Function to get the matrix
get <- function() x
# Function to set the cached inverse
setinverse <- function(inverse) inv <<- inverse
# Function to get the cached inverse
getinverse <- function() inv
# Return a list of the above functions
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
cacheSolve <- function(x, ...) {
inv <- x$getinverse() # Check if inverse is already cached
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
# If not cached, compute the inverse
mat <- x$get()
inv <- solve(mat, ...)
x$setinverse(inv) # Cache the inverse
inv
}