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
44 lines (37 loc) · 1.06 KB
/
cachematrix.R
File metadata and controls
44 lines (37 loc) · 1.06 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
## Cache a matrix inverse
##
## Usage:
## cacheMatrix <- makeCacheMatrix(matrix)
## solution <- cacheSolve(cacheMatrix)
## makeCacheMatrix() returns an instance with a pair of getters and setters
## for access to a matrix and its calculated inverse.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setCached <- function(cached) m <<- cached
getCached <- function() m
list(set = set, get = get,
setCached = setCached,
getCached = getCached)
}
## cacheSolve() returns the inverse of the matrix contained
## within the passed cacheMatrix instance.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
solution <- x$getCached()
if(!is.null(solution)) {
message("getting cached data")
return(solution)
}
data <- x$get()
solution <- solve(data, ...)
# alternative inverse matrix solution:
#library(MASS)
#solution <- ginv(data, ...)
x$setCached(solution)
solution
}