-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
61 lines (51 loc) · 1.72 KB
/
cachematrix.R
File metadata and controls
61 lines (51 loc) · 1.72 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
61
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
}
## Put comments here that give an overall description of what your
## functions do
## Function to make 'cache' matrix from a given matrix
makeCacheMatrix <- function(x = matrix()) {
#initialize cache matrix and set it to null
cacheMatrix <- NULL
#method setMatrix()
setMatrix <- function(y) {
x <<- y
cacheMatrix <<- NULL
}
#method getMatrix(), returns: matrix.
getMatrix() <- function() x
#method setCache()
setCache <- function(inverse) cacheMatrix <<- inverse
#method getCache()
getCache <- function() cacheMatrix
#listing named of methods that will be publicly available
list(setMatrix = setMatrix,
getMatrix = getMatrix,
setCache = setCache,
getCache = getCache)
}
## Function to calculate inverse of 'cache' matrix
## If inverse is already calculated, it returns the data form cache
## If inverse is not calcualted, it calculates and returns the matrix inverse
cacheSolve <- function(x, ...) {
#Checking for content of the cache matrix
cacheMatrix <- x$getCache()
#If the content is not null
if (!is.null(cacheMatrix)) {
message("Load: cache matrix.")
return(cacheMatrix)
}
#If there is no content, get matrix, create, set, update, return cache matrix
else {
dMatrix <- x$getMatrix()
cacheMatrix <- solve(dMatrix, ...)
x$setCache(cacheMatrix)
return(cacheMatrix)
}
}