-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
55 lines (51 loc) · 2.08 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
## ver 0.5
## Calculate Inverse of a Matrix using cache.
## If cached Inverse of a Matrix exists in the memory, then display it.
## Or, there is no one, calculate it.
## To do this, use a special function containing 4 sub-functions.
## "makeCacheMatrix": This function contains 4 sub-functions in it.
## Why use this complicating function?
## -> To hold the calculation results in memory without noticing it.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
# set <- function(y) {
# x <<- y
# #m <<- NULL
#}
get <- function() {
x
}
setInverse <- function(Inverse) {
m <<- Inverse
}
getInverse <- function() {
m
}
list(#set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Cacalute the inverse of of a matrix .
## If it was already calculated and cashed, then the cached results are simply returned.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse() # get the cashed data
if(!is.null(m)) { # If cashed,
message("getting cached data")
return(m)
}
data <- x$get() # If there is no cashed data, then use the new data
m <- solve(data, ...)# Calculate 'Inverse of a Matrix'
x$setInverse(m) # Cashe the solved matrix into memory.
m
}
## An example how to use the functions
matrix.1 <- c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0)
matrix.1 <- matrix(matrix.1, ncol=5, byrow=TRUE)
fx.inverse <- makeCacheMatrix(matrix.1)
matrix.1 #original matrix
cacheSolve(fx.inverse) #New solved matrix (You will not see any message except solved matrix)
cacheSolve(fx.inverse) #CASHED solved matrix. You wil see "getting cached data".
matrix.1%*%cacheSolve(fx.inverse) #check whether the solved matrix is correct
#(If you see diagnonal matrix, then it's correct!).