forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcachematrix.R
More file actions
28 lines (23 loc) · 778 Bytes
/
cachematrix.R
File metadata and controls
28 lines (23 loc) · 778 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
## Functions to solve the inverse of a matrix and cache the result.
## Creates a container that can hold a matrix and its cached inverse.
makeCacheMatrix <- function(x = matrix()) {
xinv <- NULL
set <- function(y) {
xinv <<- NULL
x <<- y
}
list(set = set, get = function() x,
setinverse = function(y) xinv <<- y, getinverse = function() xinv)
}
## Solves and caches the inverse of the matrix, so that repeated calls return the cached result until the matrix is changed.
cacheSolve <- function(x, ...) {
xinv <- x$getinverse()
if (is.null(xinv)) {
message("solving for the inverse")
xinv <- solve(x$get(), ...)
x$setinverse(xinv)
} else {
message("using cached solution")
}
xinv
}