forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
48 lines (40 loc) · 1.46 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
### Gene Cavagnaro
### R Programming on Coursera
### Programming Assignment 2
### November 9, 2022
## makeCacheMatrix: argument is a matrix. Returns a list of functions to:
# set: store/modify the matrix
# get: retrieve the matrix
# setinverse: store/modify the inverse (called by cacheSolve)
# getinverse: retrieve the inverse if previously set by setinverse.
## cacheSolve: argument is list of functions (as returned by makeCacheMatrix)
# returns the inverse of the matrix, storing it if necessary.
## Cache a matrix x and return a list of functions to access matrix and inverse.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
# if the matrix's value is reset, forget the cached inverse
}
get <- function() x
setinverse <- function(inverted) inv <<- inverted
# problem: setinverse could get called outside cacheSolve.
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Retrieve stored inverse of matrix, or else compute and store inverse.
cacheSolve <- function(x) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setinverse(inv)
inv
}