forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
33 lines (31 loc) · 1.08 KB
/
cachematrix.R
File metadata and controls
33 lines (31 loc) · 1.08 KB
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
## cacheSolve works objects created with makeCacheMatrix to
## store previously computed results of matrix inverses
## makeCacheMatrix sets up a matrix object via a list
## to store itself and a cache of its inverse
makeCacheMatrix <- function(x = matrix()) {
mi <- NULL
set <- function(y) {
x <<- y
mi <<- NULL
}
get <- function() x
setmatrix_inv <- function(solvex) mi <<- solvex
getmatrix_inv <- function() mi
list(set = set, get = get,
setmatrix_inv = setmatrix_inv,
getmatrix_inv = getmatrix_inv)
}
## cacheSolve first checks if the matrix's inverse has previously been
## computed and cached to be returned. Computes and cache the inverse otherwise.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
mi <- x$getmatrix_inv()
if(!is.null(mi)) {
message("getting cached data")
return(mi)
}
data <- x$get()
mi <- solve(data, ...)
x$setmatrix_inv(mi)
mi
}