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 (45 loc) · 1.76 KB
/
Copy pathcachematrix.R
File metadata and controls
54 lines (45 loc) · 1.76 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
## Two functions that combine to cache and call that cache
## A function that makes a special matrix that can cache an inversion of that matrix
makeCacheMatrix <- function(x = matrix()) {
## Creating the i variable to call later
i <- NULL
## creating our setter function
set <- function(y) {
x <<- y
i <<- NULL
}
## getter function to call the matrix
get <- function() x
setInvert <- function(invert){
i <<- invert
}
## Method to get the inverse of the matrix
getInvert <- function() {
## Return the inverse property
i
}
##Create a list to give names to the functions above
list(set = set, get = get,
setInvert = setInvert,
getInvert = getInvert)
}
## cacheSolve function uses the special matrix made by makeCacheMatrix.
## It first checks to see if the inverse matrix has been stored in 'i'
## and that the matrix is the same. If not it will solve the matrix and
## store that solution in 'i' in makeCacheMatrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x' if already set
i <- x$getInvert()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## takes x object and stores it in 'data'
data <- x$get()
## Use the solve function to invert the matrix
i <- solve(data) %*% data
## sets the i variable to solved inverted matrix
x$setInvert(i)
## returns the inverted matrix
i
}