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
31 lines (20 loc) · 792 Bytes
/
cachematrix.R
File metadata and controls
31 lines (20 loc) · 792 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
## makeCacheMatrix: receives a square matrix "x" and caches its inverse
## It produces a list where $get_mat (get the matrix "x"),
## $get_inv (get the inverse of "x")
makeCacheMatrix <- function(x = matrix()) {
inv <- solve(x)
get_mat <- function() x # Get the value of matrix "x"
get_inv <- function() inv # Get the cached inverse of "x"
list(get_inv=get_inv,get_mat=get_mat)
}
## cacheSolve: receives a special list (generated by makeCacheMatrix) and get the matrix inverse
## from the cached data. Otherwise, it calculates the inverse.
cacheSolve <- function(x, ...){
inv <- x$get_inv()
if(!is.null(inv)){
message("Getting cached inverse!")
return(inv)
}
inv2 <- solve(x$get_mat())
inv2
}