forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
61 lines (47 loc) · 1.55 KB
/
cachematrix.R
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## The following functions are used to cache squared matrix
## inversion. Avoiding costly unnecessary computation.
## makeCacheMatrix creates a special "matrix", which is really
## a list containing functions to:
## 1. set the data of the matrix
## 2. get the data of the matrix
## 3. set the data of the inverted matrix (solved)
## 4. get the data of the inverted matrix (solved)
makeCacheMatrix <- function(x = matrix()) {
## First time creating, set the solved as NULL
s <- NULL
## setDate is used to update the data of the "matrix"
## set the solved back to NULL
setData <- function(y) {
x <<- y
s <<- NULL
}
## return the data of the matrix
getData <- function () x
## setSolved is used to set the data of the solved
setSolved <- function (solved) s <<- solved
## return the data of the solved
getSolved <- function () s
## list of possible functions to call
list(setData = setData,
getData = getData,
setSolved = setSolved,
getSolved = getSolved)
}
## cacheSolve calculates the inverted matrix (solved) of
## the special matrix created with the above function.
cacheSolve <- function(x, ...) {
## Get the inverted matrix of 'x'
s <- x$getSolved()
## Check to see if there is a cached value
if (!is.null(s)) {
## If so, return the cached value that is the inverse of 'x'
return(s)
}
## Otherwise, calculate it for the first time
data <- x$getData()
s <- solve(data)
## And then, save it in the cache
x$setSolved(s)
## Return a matrix that is the inverse of 'x'
s
}