Skip to content

Commit 0e0c0f3

Browse files
committed
Update cachematrix.R
1 parent a21631e commit 0e0c0f3

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

cachematrix.R

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ makeCacheMatrix <- function(x = matrix()) {
2424
## If the object is not stored in cached it is created and set to cached and retuned to calling context.
2525
cacheSolve <- function(x, ...)
2626
{
27-
## Get the inverse value of 'x'
28-
invValX <- x$getinverse()
29-
if (!is.null(invValX))
30-
{
31-
message("The cached value is returned.")
32-
return(invValX)
33-
} else {
34-
invValX <- solve(x$get())
27+
## Get the inverse value of 'x' if any
28+
invValX <- x$getinverse()
29+
30+
## is there any object in cache?
31+
if (!is.null(invValX))
32+
{
33+
message("The cached value is returned.")
34+
return(invValX)
35+
}
36+
37+
## Cache is empty, calculate the value and store it in cache
38+
invValX <- solve(x$get())
39+
#set the object into cache
3540
x$setinverse(invValX)
36-
}
3741

38-
return(invValX)
42+
return(invValX)
3943
}

0 commit comments

Comments
 (0)