forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
47 lines (40 loc) · 1.44 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
## Functions to cache matrix inversion.
##
## The functions in this module provide a convenient interface
## to access and cache the inverse of a matrix. Example usage:
##
## # Create an object to store the matrix and cache its inverse
## cacheableMatrix = makeCacheMatrix( rbind(c(10, 20), c(30, 40)) )
##
## # The first call to cacheSolve will calculate the inverse,
## inverse1 = cacheSolve(cacheableMatrix)
##
## # Further calls to cacheSolve will be significantly faster, since
## # they return the value cached during the previous call.
## inverse2 = cacheSolve(cacheableMatrix)
## makeCacheMatrix -- creates an object to hold a matrix and (eventually)
## its cached inverse.
makeCacheMatrix <- function(x = matrix()) {
cachedinverse <- NULL
set <- function (newx) {
x <<- newx
cachedinverse <<- NULL
}
get <- function() x
setcachedinverse <- function(inverse) cachedinverse <<- inverse
getcachedinverse <- function() cachedinverse
list(set = set, get = get, setcachedinverse = setcachedinverse, getcachedinverse = getcachedinverse)
}
## cacheSolve -- returns the inverse of the matrix stored in a
## 'makeCacheMatrix' structure. Further calls to cacheSolve() for
## the same 'makeCacheMatrix' structure will return a cached inverse.
cacheSolve <- function(x, ...) {
cached <- x$getcachedinverse()
if (! is.null(cached)) {
return(cached)
}
m <- x$get()
cached <- solve(m)
x$setcachedinverse(cached)
cached
}