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
54 lines (40 loc) · 1.5 KB
/
Copy pathcachematrix.R
File metadata and controls
54 lines (40 loc) · 1.5 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
## These are functions to create and access an object containing a
## matrix and its inverse. To save processing time, the matrix inverse is
## cached when possible for future accesses.
## Create, initialize, and return the object to store and access the matrix
# and its inverse.
makeCacheMatrix <- function(cached_matrix = matrix()) {
matrix_inverse <- NULL
set <- function(new_matrix) {
cached_matrix <<- new_matrix
matrix_inverse <<- NULL
}
get <- function() {
cached_matrix
}
getinverse <- function() {
matrix_inverse
}
setinverse <- function(new_inverse) {
matrix_inverse <<- new_inverse
}
list(set = set, get = get, setinverse = setinverse,
getinverse = getinverse)
}
## Calculate, store, and return the inverse of a makeCacheMatrix matrix
## and return it. If the inverse has already been calculated and cached, then
## return that stored value.
cacheSolve <- function(x, ...) {
the_matrix <- x$get()
if(is.na(the_matrix[1][1])) {
stop("Atempting to retrieve inverse of an empty matrix")
}
result <- x$getinverse()
if(is.null(result)) {
result <- solve(the_matrix, ...)
x$setinverse(result)
} else {
message("getting cached data")
}
result
}