forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
47 lines (42 loc) · 1.36 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
42
43
44
45
46
47
## This file consists of two functions: makeCacheMatrix and cacheSolve. These
## two functions can be used together to create a matrix that is capable of
## caching a solved inverse of the given matrix. This can save time when the
## inverse of a matrix is needed multiple times in a piece of code.
## The makeCacheMatrix function needs a matrix as argument, and attaches
## 4 functions on the given matrix, namely:
## * set
## * get
## * setInverse
## * getInverse
## These four functions be used to set or get the matrix, and set or get the
## inverse of that matrix.
makeCacheMatrix <- function(x = matrix())
{
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInverse <- function(inverse) m <<- inverse
getInverse <- function() m
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The cacheSolve function can be used with a matrix constructed using the
## makeCacheMatrix function. This function computes the inverse of the matrix
## using the solve() function. If the inverse was already computed, it returns
## the cached inverse.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setInverse(m)
m
}