forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
49 lines (46 loc) · 1.37 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
## Creates a list containing a function to
## set the value of the matrix
## get the value of the matrix
## set the value of the inverse matrix
## get the value of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
r <- NULL
set <- function(y) {
x <<- y
r <<- NULL
}
get <- function() x
setsolve <- function(solve) r <<- solve
getsolve <- function() r
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## 1. Try to get the inverse matrix from the cache made by makeCacheMatrix above.
## 2. If the inverse has already been calculated (and the matrix has not changed), return the cache value
## 3. Otherwise, get not calculatef matrix out and culculate the inverse matrix
## 4. Set the inverse matrix into cache for further use
## 5. Then return the inverse matrix
cacheSolve <- function(x, ...) {
r <- x$getsolve()
if(!is.null(r)) {
message("getting cached data")
return(r)
}
data <- x$get()
r <- solve(data, ...)
x$setsolve(r)
r
}
## How to verify ths works, type following command in line start with "## >" as an example
## > a<-matrix(1:4,2,2)
## > t<-makeCacheMatrix(a)
## > cacheSolve(t)
# [,1] [,2]
# [1,] -2 1.5
# [2,] 1 -0.5
## > cacheSolve(t)
# getting cached data
# [,1] [,2]
# [1,] -2 1.5
# [2,] 1 -0.5