Skip to content

Commit 09048bc

Browse files
committed
more comments.
1 parent 20df2c9 commit 09048bc

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

cachematrix.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Matrix inversion is usually a costly computation and their may be some
1+
## Matrix inversion is usually a costly computation and there may be some
22
## benefit to caching the inverse of a matrix rather than compute it repeatedly.
33
## This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix)
44
## then get its inverse (using cacheSolve)
@@ -24,15 +24,22 @@
2424
makeCacheMatrix <- function(mtx = matrix()) {
2525
cachedInverse <- NULL
2626
set <- function(y) {
27+
# changes initial matrix and removes old matrix cache
2728
mtx <<- y
2829
cachedInverse <<- NULL
2930
}
3031
get <- function() mtx
31-
setInverse <- function(inv) cachedInverse <<- inv
32-
getInverse <- function() cachedInverse
33-
list(set = set, get = get,
34-
setInverse = setInverse,
35-
getInverse = getInverse)
32+
setInverse <- function(inv) {
33+
# sets inverse version of the matrix
34+
cachedInverse <<- inv
35+
}
36+
getInverse <- function() {
37+
# returns inverse of the matrix
38+
cachedInverse
39+
}
40+
list(set=set, get=get,
41+
setInverse=setInverse,
42+
getInverse=getInverse)
3643
}
3744

3845

@@ -43,8 +50,10 @@ makeCacheMatrix <- function(mtx = matrix()) {
4350
cacheSolve <- function(x, ...) {
4451
inv <- x$getInverse()
4552
if(!is.null(inv)) {
53+
# cached invers exists, return it
4654
return(inv)
4755
}
56+
# inverse is not cached yet, cache it and return
4857
mtx <- x$get()
4958
inv <- solve(mtx, ...)
5059
x$setInverse(inv)

0 commit comments

Comments
 (0)