forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
56 lines (42 loc) · 1.29 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
56
## This function computes the inverse of the matrix and stores the result in cache.
## This is the function to cache the matrix.
makeCacheMatrix <- function(x = matrix()) {
# Variable to stores the cached inverse matrix
cacheInverse <- NULL
# Function to Set/Initialize the matrix
setMatrix <- function(y) {
x <<- y
cacheInverse <<- NULL
}
# This function returns the stored matrix.
getMatrix <- function() {
x
}
# Cache the inverse of the matrix
setInverse <- function(inverse) {
cacheInverse <<- inverse
}
# Get the cached value.
getInverse <- function() {
cacheInverse
}
# Returns a list of all variables with its values.
list(setMatrix = setMatrix, getMatrix = getMatrix,
setInverse = setInverse,
getInverse = getInverse)
}
## The following function calculates the inverse of a "special" matrix created with makeCacheMatrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# Getting the cached value. If found then it returns it.
cacheInverse <- x$getInverse()
if(!is.null(cacheInverse)) {
message("getting cached data")
return(cacheInverse)
}
# Else it computes the inverse of the matrix and returns the result.
data <- x$getMatrix()
cacheInverse <- solve(data, ...)
x$setInverse(cacheInverse)
cacheInverse
}