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
59 lines (53 loc) · 1.77 KB
/
Copy pathcachematrix.R
File metadata and controls
59 lines (53 loc) · 1.77 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## Cache Matrix Inverse
###############################################################################
## This pair of functions stores the inverse of a given matrix, so that it can
## be used multiple times, while only being calculated once.
##
## For an invertible matrix A, use
## x = makeCacheMatrix(A)
## to set the matrix of interest to be A.
## Then
## cacheSolve(x)
## returns the inverse of A, while saving this inverse in the cache.
## Future calls to cacheSolve(x) return the cached value without recalculating.
## The cache is cleared when a new matrix is loaded into makeCacheMatrix.
## This function returns a list of four functions:
##
## [1] setmatrix sets the matrix of interest and clears the cache.
## [2] getmatrix returns the current matrix of interest
## [3] setinverse stores a matrix in the cache
## (which should be the inverse of the matrix of interest).
## [4] getinverse returns the matrix currently stored in the cache.
makeCacheMatrix <- function(x = matrix())
{
I <- NULL
setmatrix <- function(A)
{
x <<- A
I <<- NULL
}
getmatrix <- function() {x}
setinverse <- function(B) {I <<- B}
getinverse <- function() {I}
list(setmatrix = setmatrix, getmatrix = getmatrix,
setinverse = setinverse, getinverse = getinverse)
}
## This function returns the inverse of a matrix of interest.
## Input is a list of 4 functions produced by the makeCacheMatrix function.
##
## If there is a matrix stored in the cache, this is used for the output.
## If there is nothing in the cache, the inverse of the matrix of interest is calculated
## and stored in the cache, before being returned.
cacheSolve <- function(x, ...)
{
I <- x$getinverse()
if(!is.null(I))
{
message("getting cached data")
return(I)
}
M <- x$getmatrix()
I <- solve(M, ...)
x$setinverse(I)
I
}