forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
50 lines (43 loc) · 1.8 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
41
42
43
44
45
46
47
48
## There are two functions in this file. WOrking together
## they will take in a matrix ("set"), retrieve the matrix ("get"),
## determine the inverse of the matrix("setinverse"), or retrive the
## inverse of the matrix("getinverse")
## makeCacheMatrix will:
## 1) "save" a matrix that is given in the set()
## 2) "retrieve" the matrix with the get()
## 3) "cache" the inverse of the matrix
## 4) "retrieve" the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) { #Set the Matrix
x <<- y
m <<- NULL
}
get <- function(){ #Get the Matrix
x
}
setinverse <- function(inverse){ #Set the Matrix Inverse
m <<- inverse
}
getinverse <- function(){ #Get the Matrix Inverse
m
}
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve will
## 1) "calculate" the inverse of a matrix using the solve function,
## then call the setinverse finction
## 2) "return" the inverse of matrix if it was alreay calculated
cacheSolve <- function(x, ...) {
m <- x$getinverse() #Get the solved inverse matrix
if(!is.null(m)) { #Check if the inverse was already done
message("getting cached data") #Already cached
return(m) #Return the inverse
}
data <- x$get() #Get the matrix
m <- solve(data, ...) #Use solve to calculate the inverse
x$setinverse(m) #Save the inverse matrix
m #Return the inverse
}