Skip to content

Commit 202fa5d

Browse files
committed
Added comments to the code.
1 parent b08c68f commit 202fa5d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cachematrix.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111

1212
makeCacheMatrix <- function(x = matrix()) {
1313
m <- NULL
14+
# when setting up an object, we set the object and a NULL cached version
1415
set <- function(y) {
1516
x <<- y
1617
m <<- NULL
1718
}
18-
get <- function() x
19+
get <- function() x # get function returns the matrix object
20+
21+
# set the inverse of the matrix if "setinverse" is called
1922
setinverse <- function(solve) m <<- solve
23+
24+
# check for a solved version and then solve for an inverse
2025
getinverse <- function() m
2126
list(set = set, get = get,
2227
setinverse = setinverse,
@@ -31,14 +36,19 @@ makeCacheMatrix <- function(x = matrix()) {
3136
## `cacheSolve` should retrieve the inverse from the cache.
3237

3338
cacheSolve <- function(x, ...) {
34-
## Return a matrix that is the inverse of 'x'
39+
# first check to see if there is an inverse already cached
3540
m <- x$getinverse()
3641
if(!is.null(m)) {
3742
message("getting cached data")
3843
return(m)
3944
}
45+
# if the inverse has not been cached, first load the matrix object:
4046
data <- x$get()
47+
# then use the solve function based on the data object just created
4148
m <- solve(data, ...)
49+
# now set a cached version of the inverse
4250
x$setinverse(m)
51+
52+
#and finally, return the resut:
4353
m
4454
}

0 commit comments

Comments
 (0)