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
39 lines (33 loc) · 1.14 KB
/
cachematrix.R
File metadata and controls
39 lines (33 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
38
39
## This pair of functions can be used to calculate the inverse of a matrix
## once, cache it, and then retrieve it when it's needed later. Calculating
## the inverse can be time consuming, so it's a good idea to calculate it
## just once.
## Returns a matrix that can cache the inverse.
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() x
setinverse <- function(anInverse) inverse <<- anInverse
getinverse <- function() inverse
m <- matrix( c( set, get, setinverse, getinverse ), nrow=2, ncol=2 )
dimnames(m) <- list( c("set","get"), c("matrix","inverse") )
m
}
## Returns inverse of the input matrix. If the inverse was already calculated,
## it returns the inverse from the cache; otherwise, it calculates
## the inverse, stores it in the cache, and returns it.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x[["get","inverse"]]()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x[["get","matrix"]]()
inv <- solve(data, ...)
x[["set","inverse"]](inv)
inv
}