forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
41 lines (34 loc) · 1.02 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
## Provide methods to create a matrix and calculate and
## cache the inverse of the matrix.
##
##
## Create a "matrix" whose inverse will be cached.
##
## e.g. m <- makeCacheMatrix(matrix(1:4,2))
makeCacheMatrix <- function(x = matrix()) {
cached <- NULL
setMatrix <- function(y) {
x <<- y
theinverse <<- NULL
}
getMatrix <- function() x
setInverse <- function(inverse) cached <<- inverse
getInverse <- function() cached
list(setMatrix = setMatrix, getMatrix = getMatrix, setInverse = setInverse, getInverse = getInverse)
}
##
## Calculates the inverse of a matrix created with the previous method
## makeCacheMatrix and caches the result. Returns the inverse of the matrix.
##
## e.g. inv <- cacheSolve(m)
cacheSolve <- function(x, ...) {
m <- x$getInverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
matrix <- x$getMatrix()
inverse <- solve(matrix, ...)
x$setInverse(inverse)
inverse
}