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
57 lines (46 loc) · 2.07 KB
/
cachematrix.R
File metadata and controls
57 lines (46 loc) · 2.07 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
###########################################################
# Programming Assignment 2: Caching the Inverse of a Matrix
###########################################################
# Creates a special "matrix", which is really a list containing a function to:
# (1) set the value of the (invertible) matrix
# (2) get the value of the (invertible) matrix
# (3) set the value of the inverse matrix
# (4) get the value of the inverse matrix
makeCacheMatrix <- function(matrix = matrix()) {
# Cached value of the inverse matrix
inverseMatrix <- NULL
# (1) set the value of the (invertible) matrix
setMatrix <- function(originalMatrix) {
matrix <<- originalMatrix
inverseMatrix <<- NULL
}
# (2) get the value of the (invertible) matrix
getMatrix <- function() matrix
# (3) set the value of the inverse matrix
setInverse <- function(solve) inverseMatrix <<- solve(matrix)
# (4) get the value of the inverse matrix
getInverse <- function() inverseMatrix
list(setMatrix = setMatrix, getMatrix = getMatrix,
setInverse = setInverse,
getInverse = getInverse)
}
# Calculates the inverse of the special "matrix" created by the above function.
# It first checks to see whether the inverse has already been calculated. If
# so, it gets the inverse matrix from the cache and skips the computation.
# Otherwise, it calculates the inverse of the (invertible) matrix and sets the
# value of the inverse matrix in the cache via the setInverse function.
cacheSolve <- function(cacheMatrix, ...) {
inverseMatrix <- cacheMatrix$getInverse()
# The inverse has been calculated before
# => take the value from the cache and skip the computation
if(!is.null(inverseMatrix)) {
message("getting cached data")
return(inverseMatrix)
}
# The inverse has NOT been calculated before
# => calculate the inverse matrix and store it in the cache
data <- cacheMatrix$getMatrix()
inverseMatrix <- solve(data, ...)
cacheMatrix$setInverse(inverseMatrix)
inverseMatrix
}