File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change 4
4
# # Write a short comment describing this function
5
5
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
-
7
+ cached.result <- NULL
8
+ set <- function (y ) {
9
+ x <<- y
10
+ cached.result <<- NULL
11
+ }
12
+ get <- function () x
13
+ set.cached.result <- function (result ) cached.result <<- result
14
+ get.cached.result <- function () cached.result
15
+ list (set = set , get = get ,
16
+ set.cached.result = set.cached.result ,
17
+ get.cached.result = get.cached.result )
8
18
}
9
19
10
20
11
21
# # Write a short comment describing this function
12
22
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
23
+ cacheSolve <- function (matrix , ... ) {
24
+ res <- matrix $ get.cached.result()
25
+ if (! is.null(res )) {
26
+ message(" getting cached data" )
27
+ return (res )
28
+ }
29
+ data <- matrix $ get()
30
+ res <- solve(data )
31
+ matrix $ set.cached.result(res )
32
+ res
15
33
}
34
+
35
+ # example of invertible matrix
36
+ # hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }
37
+ # h8 <- hilbert(8)
You can’t perform that action at this time.
0 commit comments