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 (54 loc) · 1.71 KB
/
cachematrix.R
File metadata and controls
59 lines (54 loc) · 1.71 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
## cacheMatrix.R: This file contains functions to invert matricies
## and cache the result to be used on repeated calls.
##
## Ex:
##
## > source('cachematrix.R')
## > m1 <- matrix(c(1/2, -1/4, -1, 3/4), nrow = 2, ncol = 2)
## > m1
## [,1] [,2]
## [1,] 0.50 -1.00
## [2,] -0.25 0.75
## > myMatrix_object <- makeCacheMatrix(m1)
## > cacheSolve(myMatrix_object)
## [,1] [,2]
## [1,] 6 8
## [2,] 2 4
## > cacheSolve(myMatrix_object)
## getting cached data
## [,1] [,2]
## [1,] 6 8
## [2,] 2 4
## makeCacheMatrix() takes a matrix x as an argument and returns a list with
## function attributes: x$set(), x$get(), x$setinverse(), and x$getinverse().
## x$set() and x$get() set and get the matrix itself.
## x$setinverse() and x$getinverse() set and get the computed inverse.
## If the inverse has not been cached, x$getinverse() will return NULL.
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() takes the "matrix"-like object x returned by makeCacheMatrix()
## as an argument and returns a matrix that is the inverse of x.
## If the inverse has been previously computed, the cached copy is returned.
## If not, the inverse is computed on-demand, cached, and returned.
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}