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 (36 loc) · 1.14 KB
/
cachematrix.R
File metadata and controls
38 lines (36 loc) · 1.14 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
## Create a matrix object which cache its inverse
makeCacheMatrix <- function(x=matrix()){
## Initialize the inverse
inv <- NULL
## Set the matrix
set <- function(y){
x <<- y
inv <<- NULL
}
## Get the matrix
get <- function(){x}
## Set the inverse of the matrix
setInverse <- function(inverse){inv <<- inverse}
## Get the inverse of the matrix
getInverse <- function() {inv}
## Return a list of the methods
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
## Compute the inverse of the special matrix returned by "makeCacheMatrix".
cacheSolve <- function(x, ...){
## Return a inverse matrix of 'x'
inv <- x$getInverse()
## Just return the inverse if its already present
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
## Get the matrix
mat <- x$get()
## Calculate the inverse
inv <- solve(mat, ...)
## Set the inverse
x$setInverse(inv)
## Return
inv
}