File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # # Put comments here that give an overall description of what your
2- # # functions do
3-
4- # # Write a short comment describing this function
1+ # # A Matrix object that caches its inverse
52
3+ # # Creates the Matrix object with starting matrix
4+ # # $set sets the matrix
5+ # # $get returns the matrix
6+ # # $setInverse sets the inverse of the matrix
7+ # # $getInverse returns the inverse of the matrix
68makeCacheMatrix <- function (x = matrix ()) {
7- cachedInverse <- NULL
9+ cachedInverse <- NULL # holds the cached inverse
810 set <- function (newMatrix ) {
911 x <<- newMatrix
10- cachedInverse <<- NULL
12+ cachedInverse <<- NULL # reset the inverse
1113 }
1214 get <- function () x
1315 setInverse <- function (inverse ) cachedInverse <<- inverse
@@ -19,17 +21,15 @@ makeCacheMatrix <- function(x = matrix()) {
1921}
2022
2123
22- # # Write a short comment describing this function
23-
24+ # # Checks to see if the inverse is cached.
25+ # # If it isn't calculates the inverse and sets it
26+ # # Lastly returns the inverse.
2427cacheSolve <- function (x , ... ) {
25- # # Return a matrix that is the inverse of 'x'
2628 inverse <- x $ getInverse()
27- if (! is.null(inverse )) {
28- message(" getting cached data" )
29- return (inverse )
29+ if (is.null(inverse )) {
30+ data <- x $ get()
31+ inverse <- solve(data )
32+ x $ setInverse(inverse )
3033 }
31- data <- x $ get()
32- inverse <- solve(data )
33- x $ setInverse(inverse )
3434 inverse
3535}
You can’t perform that action at this time.
0 commit comments