File tree Expand file tree Collapse file tree 1 file changed +15
-15
lines changed Expand file tree Collapse file tree 1 file changed +15
-15
lines changed 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
5
2
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
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
- cachedInverse <- NULL
9
+ cachedInverse <- NULL # holds the cached inverse
8
10
set <- function (newMatrix ) {
9
11
x <<- newMatrix
10
- cachedInverse <<- NULL
12
+ cachedInverse <<- NULL # reset the inverse
11
13
}
12
14
get <- function () x
13
15
setInverse <- function (inverse ) cachedInverse <<- inverse
@@ -19,17 +21,15 @@ makeCacheMatrix <- function(x = matrix()) {
19
21
}
20
22
21
23
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.
24
27
cacheSolve <- function (x , ... ) {
25
- # # Return a matrix that is the inverse of 'x'
26
28
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 )
30
33
}
31
- data <- x $ get()
32
- inverse <- solve(data )
33
- x $ setInverse(inverse )
34
34
inverse
35
35
}
You can’t perform that action at this time.
0 commit comments