Skip to content

Commit 5100f81

Browse files
authored
Update cachematrix.R
1 parent 7f657dd commit 5100f81

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

cachematrix.R

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
51

62
makeCacheMatrix <- function(x = matrix()) {
7-
3+
inv <- NULL
4+
5+
6+
set <- function(y) {
7+
x <<- y
8+
inv <<- NULL
9+
}
10+
11+
12+
get <- function() {
13+
x
14+
}
15+
16+
17+
setInverse <- function(inverse) {
18+
inv <<- inverse
19+
}
20+
21+
22+
getInverse <- function() {
23+
inv
24+
}
25+
26+
27+
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
828
}
929

1030

11-
## Write a short comment describing this function
12-
1331
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
inv <- x$getInverse()
33+
34+
35+
if (!is.null(inv)) {
36+
message("getting cached data")
37+
return(inv)
38+
}
39+
40+
41+
mat <- x$get()
42+
inv <- solve(mat, ...)
43+
44+
45+
x$setInverse(inv)
46+
47+
return(inv)
1548
}

0 commit comments

Comments
 (0)