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
59 lines (44 loc) · 1.86 KB
/
Copy pathcachematrix.R
File metadata and controls
59 lines (44 loc) · 1.86 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
## The following functions below will help find the inverse of a matrix, if it is
## invertible, given a matrix object.
## A matrix object can be created using makeCacheMatrix() function. The created
## matrix object has capability to store both the matrix input and matrix inverse result.
## In addition, if the matrix object already had the cached inverse result, the cacheSolve()
## function will just return it without spending expensive resource to re-compute it.
## This function creates a special "matrix" object that can cache its inverse
makeCacheMatrix <- function(x = matrix()) {
invMatrix <- NULL
## Define a set() function to store matrix input data
set <- function(y){
x <<- y
invMatrix <<- NULL
}
## Define a get() function to return matrix input data
get <- function() x
## Define a getInverse() function to return the cached
## matrix inverse result, if available
getInverse <- function() invMatrix
## Define a setInverse() function to store cached result
## of inverse matrix of X
setInverse <- function(invX){
invMatrix <<- invX
}
## Define list of functions to be accessible
list(get = get, set = set,
getInverse = getInverse, setInverse = setInverse)
}
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix
## above. If the inverse has already been calculated (and the matrix has not changed),
## then cacheSolve should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invMatrix <- x$getInverse()
if (!is.null(invMatrix)){
message("Getting cached matrix inverse result")
return(invMatrix)
}
## Compute the matrix Inverse of X, assuming X is invertible
invMatrix <- solve(x$get())
## Cache the inverse matrix of X for next retrieval
x$setInverse(invMatrix)
invMatrix
}