Skip to content

Commit cb6b1d4

Browse files
committed
maybe I got it?
1 parent 7f657dd commit cb6b1d4

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

cachematrix.R

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
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
62
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+
832
}
933

1034

1135
## Write a short comment describing this function
1236

37+
38+
## Returns a matrix that is the inverse of 'x'
1339
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+
1556
}

0 commit comments

Comments
 (0)