File tree Expand file tree Collapse file tree 1 file changed +48
-7
lines changed Expand file tree Collapse file tree 1 file changed +48
-7
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
5
-
1
+ # # stores a matrix and its inverse
6
2
makeCacheMatrix <- function (x = matrix ()) {
7
-
3
+
4
+ # initialize variables
5
+ inv <- NULL
6
+
7
+
8
+ # create 4 functions:
9
+ # 1. set matrix - stores the matrix and wipes out the cache
10
+ setmatrix <- function (y ) {
11
+ inv <<- NULL
12
+ x <<- y
13
+ }
14
+
15
+ # 2. get matrix - returns the stored matrix (so you can assign the matrix, plus functions elsewhere)
16
+ getmatrix <- function () x
17
+
18
+ # 3. set inv - sets the inverse cache to whatever you tell it
19
+ setinv <- function (inverseval ) {
20
+ inv <<- inverseval
21
+ }
22
+
23
+ # 4. get inv - just returns the cached inv
24
+ getinv <- function () inv
25
+
26
+ # wrap the 4 functions up into a list
27
+ list (setmatrix = setmatrix ,
28
+ getmatrix = getmatrix ,
29
+ setinv = setinv ,
30
+ getinv = getinv )
31
+
8
32
}
9
33
10
34
11
35
# # Write a short comment describing this function
12
36
37
+
38
+ # # Returns a matrix that is the inverse of 'x'
13
39
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
40
+
41
+ # test if x$getinv is null
42
+ if (is.null(x $ getinv()) ) {
43
+ # if so, get the matrix itself...
44
+ matr <- x $ getmatrix()
45
+ # then solve for the inverse
46
+ inv <- solve(matr )
47
+
48
+ # if it's not null
49
+ } else {
50
+ # just grab the cached inverse
51
+ inv <- x $ getinv()
52
+ }
53
+
54
+ return (inv )
55
+
15
56
}
You can’t perform that action at this time.
0 commit comments