Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a722a98

Browse files
author
Erik Larson
committed
Updating comments
1 parent a127cac commit a722a98

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

cachematrix.R

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@
66
## This function creates a special "matrix" object that can cache its inverse.
77

88
makeCacheMatrix <- function(x = matrix()) {
9-
9+
## inv is initialized to NULL
1010
i <- NULL
1111

12+
## setter for the original matrix to solve and set inv back to NULL
1213
set <- function(y) {
1314
x <<- y
1415
i <<- NULL
1516
}
17+
## getter for the original matrix
1618
get <- function() x
17-
setinverse <- function(inverse) i <<- inverse
18-
getinverse <- function() i
19-
list(set = set, get = get,
19+
20+
## setter for inverse matrix
21+
setinverse <- function(inverse) i <<- inverse
22+
23+
## getter for inverse matrix
24+
getinverse <- function() i
25+
26+
## return information
27+
list(set = set, get = get,
2028
setinverse = setinverse,
2129
getinverse = getinverse)
2230
}
@@ -28,14 +36,21 @@ makeCacheMatrix <- function(x = matrix()) {
2836
## from the cache.
2937

3038
cacheSolve <- function(x, ...) {
31-
## Return a matrix that is the inverse of 'x'
39+
## get inverse matrix from the special matrix object
3240
i <- x$getinverse()
33-
if(!is.null(i)) {
41+
42+
## check if an inverse matrix is already cached, if so return the value
43+
if(!is.null(i)) {
3444
message("getting cached data")
3545
return(i)
3646
}
37-
data <- x$get()
47+
48+
## if no cached value for inverse matrix, get the original matrix and
49+
## compute the inverse
50+
data <- x$get()
3851
i <- solve(data, ...)
39-
x$setinverse(m)
52+
53+
## store and return the inverse matrix
54+
x$setinverse(m)
4055
i
4156
}

0 commit comments

Comments
 (0)