forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
40 lines (31 loc) · 952 Bytes
/
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
## makeCacheMatrix creates a list of functions to get/set a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
## set the matrix
set <- function(newMatrix) {
x <<- newMatrix
inverse <<- NULL
}
## get the matrix
get <- function() x
## set the inverse of the matrix
setInverse <- function(newInverse) inverse <<- newInverse
## get the inverse of the matrix
getInverse <- function() inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## cacheSolve returns the inverse of 'x', caching the result inside x
## x must be "matrix" created with makeCacheMatrix otherwise an error occurs
cacheSolve <- function(x, ...) {
inverse <- x$getInverse()
if(!is.null(inverse)) {
message("getting cached inverse")
return(inverse)
}
matrix <- x$get()
inverse <- solve(matrix, ...)
x$setInverse(inverse)
inverse
}