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
53 lines (41 loc) · 1.97 KB
/
cachematrix.R
File metadata and controls
53 lines (41 loc) · 1.97 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
47
48
49
50
51
52
53
## makeCacheMatrix creates a special "vector", which is really 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()) {
p_inversedMatrix <- NULL ## this variable stores the value of inversed matrix
## set the value of the matrix
set <- function(matr){
x <<- matr
p_inversedMatrix <<- NULL
}
## get the value of the matrix
get <- function() x
## set the value of the inverse matrix
setInverseMatrix <- function(inverseMatrix) {
p_inversedMatrix <<- inverseMatrix
}
## get the value of the inverse matrix
getInversedMatrix <- function() p_inversedMatrix
list(set = set, get = get, setInverseMatrix = setInverseMatrix,
getInversedMatrix = getInversedMatrix)
}
## Return a matrix that is the inverse of 'x'.
## cacheSolve function calculates the inverse matrix of the special "vector" created using makeCacheMatrix function.
## However, it first checks to see if the inverse matrix has already been calculated. If so, it gets the inverse matrix from the cache
## and skips the computation. Otherwise, it calculates the inverse matri and sets it in the cache via the setInverseMatrix function.
cacheSolve <- function(x, ...) {
p_inversedMatrix <- x$getInversedMatrix() # get the value of inversed matrix
## don't make inverse matrix calculation and return its value from cache if p_inversedMatrix is not NULL - meaning cached value exist
if (!is.null(p_inversedMatrix)) {
message("getting cached inversed matrix")
return(p_inversedMatrix)
}
## otherwise, calculate inverse matrix value and set it in cache
message("calculating inversed matrix")
data <- x$get() ## get matrix data for calculation
p_inversedMatrix <- solve(data, ...) ## calculate inverse matrix
x$setInverseMatrix(p_inversedMatrix) ## set calculated inverse matrix in cache
p_inversedMatrix
}