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
49 lines (35 loc) · 1.2 KB
/
cachematrix.R
File metadata and controls
49 lines (35 loc) · 1.2 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
## makeCacheMatrix allows a get/set for matrix creation/retrieval
makeCacheMatrix <- function(x = matrix()) {
## matrix inverse placeholder, default value NULL
m <- NULL
## Allows passing a new matrix to be created and re-sets matrix-inverse placeholder to NULL
set <- function(y) {
x <<- y
m <<- NULL
}
## gets the matrix inverse
get <- function() x
## solves matrix/creates inverse and caches matrix inverse
setmatrix <- function(solve) m <<- solve
## retrieves cached matrix inverse value
getmatrix <- function() m
## returns value of function makeCacheMatrix
list(set = set, get = get,
setmatrix = setmatrix,
getmatrix = getmatrix)
}
## creates an inverse of a matrix and caches, or retrieves it from cache if inverse has already been created
cacheSolve <- function(x=matrix(), ...) {
m <- x$getmatrix()
##retrieves cached matrix inverse, if one exists
if(!is.null(m)) {
message("getting cached data")
return(m)
}
## if no cached matrix inverse exists, creates the inverse and returns the matrix inverse.
matrix <- x$get()
m <- solve(matrix, ...)
x$setmatrix(m)
## Return a matrix that is the inverse of 'x'
m
}