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
60 lines (37 loc) · 1.15 KB
/
cachematrix.R
File metadata and controls
60 lines (37 loc) · 1.15 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
54
55
56
57
58
59
60
makeCacheMatrix <- function(x = matrix()) {
# makes a cache matrix from a given matrix
# assign the value NULL for the first initialization
cacheMatrix <- NULL
# define the function named 'setMatrix'
setMatrix <- function(y) {
x <<- y
cacheMatrix <<- NULL
}
# define the method named 'getMatrix'
getMatrix <- function() x
# define the method named 'setCache'
setCache <- function(inverse) cacheMatrix <<- inverse
# define the method named 'getCache'
# return the cached inverse of 'x'
getCache <- function() cacheMatrix
list(setMatrix = setMatrix,
getMatrix = getMatrix,
setCache = setCache,
getCache = getCache)
}
cacheSolve <- function(x, ...) {
# return the inverse of a given matrix utilizing the cache
cacheMatrix <- x$getCache()
# if the content is not null then: return the result
if (!is.null(cacheMatrix)) {
message("loading cache matrix...")
return(cacheMatrix)
}
# if the content is empty:
else {
dMatrix <- x$getMatrix()
cacheMatrix <- solve(dMatrix, ...)
x$setCache(cacheMatrix)
return(cacheMatrix)
}
}