-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachematrix.R
65 lines (61 loc) · 1.71 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## These functions calculate and return a matrix which is the inverse
## of the input matrix. If the input matrix was seen previously, a
## cached value of the inverse matrix is returned, rather than
## recalculating the inverse all over again.
## makeCacheMatrix creates a list of four functions, which set and get
## the value of a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(solve) m <<- solve
getinverse <- function() m
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
## cacheSolve returns a matrix which is the inverse of the input
## matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getinverse()
if(is.matrix(m)){
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}
## Some examples to cut-n-paste:
## > A <- matrix(1:4, nrow = 2)
## > A
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## > a <- makeCacheMatrix(A)
## > b <- matrix(c(3,2,1,1,1,-1,0,1,2), nrow=3)
## > b
## [,1] [,2] [,3]
## [1,] 3 1 0
## [2,] 2 1 1
## [3,] 1 -1 2
## > B <- matrix(c(3,2,1,1,1,-1,0,1,2), nrow=3)
## > b <- makeCacheMatrix(B)
## > cacheSolve(b)
## [,1] [,2] [,3]
## [1,] 0.5 -0.3333333 0.1666667
## [2,] -0.5 1.0000000 -0.5000000
## [3,] -0.5 0.6666667 0.1666667
## > cacheSolve(b)
## [,1] [,2] [,3]
## [1,] 0.5 -0.3333333 0.1666667
## [2,] -0.5 1.0000000 -0.5000000
## [3,] -0.5 0.6666667 0.1666667
## >