File tree 1 file changed +23
-8
lines changed 1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change 6
6
# # This function creates a special "matrix" object that can cache its inverse.
7
7
8
8
makeCacheMatrix <- function (x = matrix ()) {
9
-
9
+ # # inv is initialized to NULL
10
10
i <- NULL
11
11
12
+ # # setter for the original matrix to solve and set inv back to NULL
12
13
set <- function (y ) {
13
14
x <<- y
14
15
i <<- NULL
15
16
}
17
+ # # getter for the original matrix
16
18
get <- function () x
17
- setinverse <- function (inverse ) i <<- inverse
18
- getinverse <- function () i
19
- list (set = set , get = get ,
19
+
20
+ # # setter for inverse matrix
21
+ setinverse <- function (inverse ) i <<- inverse
22
+
23
+ # # getter for inverse matrix
24
+ getinverse <- function () i
25
+
26
+ # # return information
27
+ list (set = set , get = get ,
20
28
setinverse = setinverse ,
21
29
getinverse = getinverse )
22
30
}
@@ -28,14 +36,21 @@ makeCacheMatrix <- function(x = matrix()) {
28
36
# # from the cache.
29
37
30
38
cacheSolve <- function (x , ... ) {
31
- # # Return a matrix that is the inverse of 'x'
39
+ # # get inverse matrix from the special matrix object
32
40
i <- x $ getinverse()
33
- if (! is.null(i )) {
41
+
42
+ # # check if an inverse matrix is already cached, if so return the value
43
+ if (! is.null(i )) {
34
44
message(" getting cached data" )
35
45
return (i )
36
46
}
37
- data <- x $ get()
47
+
48
+ # # if no cached value for inverse matrix, get the original matrix and
49
+ # # compute the inverse
50
+ data <- x $ get()
38
51
i <- solve(data , ... )
39
- x $ setinverse(m )
52
+
53
+ # # store and return the inverse matrix
54
+ x $ setinverse(m )
40
55
i
41
56
}
You can’t perform that action at this time.
0 commit comments