forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
43 lines (37 loc) · 1.29 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
## The following two functions "makeCacheMatrix" and "cacheSolve" can be used together to
## avoid the repeated invocation of the time consuming "solve" operation.
## e.g.
## m <- makeCacheMatrix(matrix(rnorm(9), 3, 3))
## m.1 <- cacheSolve(m)
## Creates a wrapper around a matrix that can store
## the result of a time consuming operation.
## The input matrix can be read and modified with the $get and $set functions
## The cached results can be accessed with $get.cache and set with $set.cache
makeCacheMatrix <- function(x = matrix()) {
cache <- NULL
set <- function(y) {
x <<- y
cache <<- NULL
}
get <- function() x
set.cache <- function(data) cache <<- data
get.cache <- function() cache
list(set = set, get = get,
set.cache = set.cache,
get.cache = get.cache)
}
## Solves the inverse of a matrix that has been wrapped
## with the function "makeCacheMatrix".
## The result of the solve function is returned and stored in the given matrix.
## The same result is returned on sub sequent invocations of this function.
## All additional parameters are passed to the solve function.
cacheSolve <- function(x, ...) {
inv <- x$get.cache()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
inv <- solve(x$get(), ...)
x$set.cache(inv)
inv
}