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
37 lines (31 loc) · 1.54 KB
/
Copy pathcachematrix.R
File metadata and controls
37 lines (31 loc) · 1.54 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
## These functions allow for the cacheing of the inverse (solution) to a matrix, so
## subesquent solves can be performed much more quickly. These funtions create a special
## object that stores a matrix and caches it's inverse.
## This function creats a list that contains functions to set and get a matrix and get and set
## the inverse of the matrix.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL # m will store the inverse of the matrix. It is NULL if the inverse
# has not yet been cached
set <- function(y){ #defines the values/size of the matrix
x <<- y
m <<- NULL
}
get <- function() x # returns the matrix
setsolve <- function(solve) m <<- solve # solves and caches the solution
getsolve <- function() m # retrieves the solution from the cache
list(set = set, get = get, setsolve = setsolve, getsolve = getsolve)
}
## This function will get the cached solution to the matrix if it has been previously solved,
## or solve and chache the solution if it hasn't been.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getsolve() # sets m to the solution if the matrix has been solved previously
if(!is.null(m)) {
message("getting cached data") # Returns the chached solution
return(m)
}
data <- x$get()
m <- solve(data, ...) # Solves the matrix if it hasn't been solved before
x$setsolve(m) # Caches the solution.
m
}