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

Skip to content

Commit d640fdd

Browse files
author
cad_rboliveira
committed
editing file to include my solution for cached inversed matrix
1 parent 7f657dd commit d640fdd

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cachematrix.R

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,38 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inversedM <- NULL
8+
9+
set <- function(y) {
10+
x <<- y
11+
inversedM <<- NULL
12+
}
13+
14+
get <- function() x
15+
16+
setInversed <- function(inversedM) inversedM <<- inversedM
17+
18+
getInversed <- function() inversedM
19+
20+
list(set = set, get = get, setInversed = setInversed, getInversed = getInversed)
821
}
922

1023

1124
## Write a short comment describing this function
1225

1326
cacheSolve <- function(x, ...) {
1427
## Return a matrix that is the inverse of 'x'
28+
inverserd <- x$getInversed()
29+
if (!is.null(inverserd)) {
30+
message("getting cached data")
31+
return(inverserd)
32+
}
33+
34+
matrix <- x$get()
35+
36+
inversed <- solve(matrix)
37+
38+
x$setInversed(inversed)
39+
40+
inversed
1541
}

0 commit comments

Comments
 (0)