File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change 11
11
12
12
makeCacheMatrix <- function (x = matrix ()) {
13
13
m <- NULL
14
+ # when setting up an object, we set the object and a NULL cached version
14
15
set <- function (y ) {
15
16
x <<- y
16
17
m <<- NULL
17
18
}
18
- get <- function () x
19
+ get <- function () x # get function returns the matrix object
20
+
21
+ # set the inverse of the matrix if "setinverse" is called
19
22
setinverse <- function (solve ) m <<- solve
23
+
24
+ # check for a solved version and then solve for an inverse
20
25
getinverse <- function () m
21
26
list (set = set , get = get ,
22
27
setinverse = setinverse ,
@@ -31,14 +36,19 @@ makeCacheMatrix <- function(x = matrix()) {
31
36
# # `cacheSolve` should retrieve the inverse from the cache.
32
37
33
38
cacheSolve <- function (x , ... ) {
34
- # # Return a matrix that is the inverse of 'x'
39
+ # first check to see if there is an inverse already cached
35
40
m <- x $ getinverse()
36
41
if (! is.null(m )) {
37
42
message(" getting cached data" )
38
43
return (m )
39
44
}
45
+ # if the inverse has not been cached, first load the matrix object:
40
46
data <- x $ get()
47
+ # then use the solve function based on the data object just created
41
48
m <- solve(data , ... )
49
+ # now set a cached version of the inverse
42
50
x $ setinverse(m )
51
+
52
+ # and finally, return the resut:
43
53
m
44
54
}
You can’t perform that action at this time.
0 commit comments