forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
40 lines (29 loc) · 1.26 KB
/
cachematrix.R
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
## The functions contained here work together to create a matrix, invert it, save the inversion to the cache, and then retrieve the inversion if it exists.
## Takes one argument named x, this argument must be a matrix and must be invertable.
## It first sets the matrix then inverts it
makeCacheMatrix <- function(x = matrix()) {
mtx <- NULL
setMatrix <- function(y) {
x <<- y
mtx <<- NULL
}
getMatrix <- function() x
invertMatrix <- function(inverted) mtx <<- inverted
getInversion <- function() mtx
print(list(setMatrix = setMatrix, getMatrix = getMatrix, invertMatrix = invertMatrix, getInversion = getInversion))
}
## This function takes one argument named x, this argument must be a matrix and it must be invertable.
## First it checks to see if the inversion of the matrix is already in the cache, if it is it returns the cached data, otherwise it computes the inverse of the special matrix object
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
mtx <- x$getInversion()
if(!is.null(mtx)) {
message("Getting cached data")
return(mtx)
}
data <- x$getMatrix()
mtx <- solve(data)
x$invertMatrix(mtx)
mtx
}
myMatrix <- matrix(c(1,3,2,4,7,5,3,8,9), nrow = 3, ncol = 3)