Skip to content

Commit 2a85a46

Browse files
committed
Added comments to functions
1 parent 9909095 commit 2a85a46

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cachematrix.R

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
1-
# Create an object with new
1+
# Create a list object as to be able to calculate
2+
# Inverse Matrix using cache
23
makeCacheMatrix <- function(m = matrix())
34
{
45
inv <- NULL
56

7+
# Set new matrix object and make cached inverse NULL
68
setMatrix <- function(new_m)
79
{
810
m <<- new_m
911
inv <<- NULL
1012
}
1113

14+
# get matrix object
1215
getMatrix <- function () m
1316

17+
# Set Inverse matrix to a global variable as to use
18+
# it for inverse cache
1419
setInverse <- function(new_inv) inv <<- new_inv
15-
getInverse <- function()a < inv
1620

21+
# get cached Inversed Matrix
22+
getInverse <- function() inv
23+
24+
# return list with function
1725
list(setMatrix = setMatrix,
1826
getMatrix = getMatrix,
1927
setInverse = setInverse,
2028
getInverse = getInverse)
2129
}
2230

31+
# This function should take as argument a list that be created
32+
# using makeCacheMatrix
2333
cacheSolve <- function(m, ...)
2434
{
25-
## Return a matrix that is the inverse of 'x'
35+
# First take cached Inversed Matrix
2636
inv <- m$getInverse()
37+
38+
# if the cached Inversed matrix is not NULL then return it
39+
# and take advantage from cache
2740
if(!is.null(inv))
2841
{
2942
message("*** Inverse Matrix is from cache ***")
3043
return(inv)
3144
}
45+
46+
# otherwise get matrix
3247
temp_matrix <- m$getMatrix()
48+
49+
# calculate inverse matrix
3350
inv <- solve(temp_matrix)
51+
52+
# put new calculated inversed matrix to cache
53+
# for future use if call this function again
54+
# and matrix do not cache
3455
m$setInverse(inv)
3556
inv
3657
}

0 commit comments

Comments
 (0)