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
46 lines (41 loc) · 1.49 KB
/
cachematrix.R
File metadata and controls
46 lines (41 loc) · 1.49 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
34
35
36
37
38
39
40
41
42
43
44
45
46
## These functions take a matrix and get it's inverse, if it is not given, in an special
## list. The matrix and it's inverse get stored in the list so they can be easily accessed
## by the $ operator.
## First function: creates a functional list that stores a matrix and it's inverse, if given.
## If there's no argument, a list of four functions is created, ready to store the needed data:
## set: sets the matrix if it wasn't given in the argument.
## get: displays the stored matrix in the list.
## set_inv: sets the inverse of the matrix if it is available.
## get_inv: displays the inverse of the matrix given.
makeCacheMatrix <- function(x = matrix()) {
b <- NULL
set <- function(y) {
x <<- y
b <<- NULL
}
get <- function() {
x
}
set_inv <- function(inv_x) {
b <<- inv_x
}
get_inv <- function() {
b
}
list(set = set, get = get, set_inv = set_inv, get_inv = get_inv)
}
## Second function: given the functional list created with "makeCacheMatrix", it reads the
## list, if the inverse of a matrix was given in the list, then a message is given and the
## matrix is displayed. Else, it is calculated first, stored in the list with the help of
## it's third element and then it is displayed.
cacheSolve <- function(x, ...) {
b <- x$get_inv()
if(!is.null(b)) {
message("getting cached data")
return(b)
}
data <- x$get()
b <- solve(data, ...)
x$set_inv(b)
b
}