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
31 lines (28 loc) · 2.12 KB
/
Copy pathcachematrix.R
File metadata and controls
31 lines (28 loc) · 2.12 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
## makeCacheMatrix creates a special matrix object, and then cacheSolve calculates the inverse of the matrix.
## If the matrix inverse has already been calculated, it will instead find it in the cache and return it, and not calculate it again.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL ## sets the value of m to NULL (provides a default if cacheSolve has not yet been used)
set <- function(y) { ##set the value of the matrix
x <<- y ## caches the inputted matrix so that cacheSolve can check whether it has changed
m <<- NULL ## sets the value of m (the matrix inverse if used cacheSolve) to NULL
}
get <- function() x #getting the value of the matrix
setmatrix <- function(solve) m <<- solve #setting the value of the inverse of the matirx
getmatrix <- function() m #getting the value of the inverse of the matrix
list(set = set, get = get,
setmatrix = setmatrix,
getmatrix = getmatrix)
}
## The function cacheSolve returns the inverse of a matrix A created with the makeCacheMatrix function.
## If the cached inverse is available, cacheSolve retrieves it, while ifnot, it computes, caches, and returns it.
cacheSolve <- function(x, ...) {
m <- x$getmatrix() ## if an inverse has already been calculated this gets it
if(!is.null(m)) { ##check to see if cacheSolve has been run before
message("getting cached data")
return(m)
}
matrix <- x$get() ## run the getmatrix function to get the value of the input matrix
m <- solve(matrix, ...) ## compute the value of the inverse of the input matrix
x$setmatrix(m) ## run the setmatrix function on the inverse to cache the inverse
m ## return the inverse
}