Skip to content

Commit 9c9139a

Browse files
committed
Commented and refactored
1 parent 7131bfa commit 9c9139a

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

cachematrix.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
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
68
makeCacheMatrix <- 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.
2427
cacheSolve <- 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
}

0 commit comments

Comments
 (0)