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
38 lines (33 loc) · 1.15 KB
/
Copy pathcachematrix.R
File metadata and controls
38 lines (33 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
## Create an invertible matrix and cache the inverse of the matrix.
## The 1st function creates a "Matrix". which is a list containing a function to:
## 1. set the value of the matrix
## 2. get the value of the matrix
## 3. set the value of the inverse
## 4. get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
#Initialize the inverse value
xInverse <- NULL
set <- function(y) {
x <<- y
xInverse <<- NULL
}
get <- function() x
setInverse <- function(solve) xInverse <<- solve
getInverse <- function() xInverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The 2nd function solve the inverse of the matrix created with the 1st function.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
xInverse <- x$getInverse()
if(!is.null(xInverse)) {
message("getting cached data")
return(xInverse)
}
data <- x$get()
xInverse <- solve(data, ...)
x$setInverse(xInverse)
xInverse
}