File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
1
+ # # Builds a special Matrix that can cache the inverse of itself when
2
+ # # using the cacheSolve function
5
3
6
4
makeCacheMatrix <- function (x = matrix ()) {
7
5
i <- NULL
6
+ # set the matrix and clear the cached value
8
7
set <- function (y ) {
9
8
x <<- y
10
9
i <<- NULL
11
10
}
12
11
get <- function () x
12
+ # cache the inverse in i
13
13
setInverse <- function (inverse ) i <<- inverse
14
+ # retieve cached value
14
15
getInverse <- function () i
15
16
list (set = set , get = get ,
16
17
setInverse = setInverse ,
17
18
getInverse = getInverse )
18
19
}
19
20
20
21
21
- # # Write a short comment describing this function
22
+ # # Using the makeCacheMatrix solve the matrix and cache it
23
+ # # so that multiple cacheSolve calls will not perform the expensive
24
+ # # solve operation.
22
25
23
26
cacheSolve <- function (x , ... ) {
24
27
# # Return a matrix that is the inverse of 'x'
28
+
29
+ # check to see if it's cached
25
30
i <- x $ getInverse()
26
31
if (! is.null(i )) {
27
32
message(" getting cached value" )
28
33
return (i )
29
34
}
35
+ # not cached, so solve and store the value in the cache
30
36
data <- x $ get()
31
37
i <- solve(data , ... )
32
38
x $ setInverse(i )
You can’t perform that action at this time.
0 commit comments