|
2 | 2 | ## functions do
|
3 | 3 |
|
4 | 4 | ## Write a short comment describing this function
|
| 5 | +## The function sets and gets the matrix and its inverse |
5 | 6 |
|
6 | 7 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 8 | + m <- NULL |
| 9 | + set <- function(y) { |
| 10 | + x <<- y |
| 11 | + m <<- NULL |
| 12 | + } |
| 13 | + get <- function() x |
| 14 | + setinverse <- function(solve) m <<- solve |
| 15 | + getinverse <- function() m |
| 16 | + list(set = set, get = get, |
| 17 | + setinverse = setinverse, |
| 18 | + getinverse = getinverse) |
8 | 19 | }
|
9 | 20 |
|
10 | 21 |
|
11 | 22 | ## Write a short comment describing this function
|
| 23 | +## the function calculates the inverse of the matrix created above. |
| 24 | +## checks to see if inverse is empty and if not uses cached inverse |
12 | 25 |
|
13 | 26 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 27 | + m <- x$getinverse() |
| 28 | + if(!is.null(m)) { |
| 29 | + message("getting cached data") |
| 30 | + return(m) |
| 31 | + } |
| 32 | + data <- x$get() |
| 33 | + m <- solve(data, ...) |
| 34 | + x$setinverse(m) |
| 35 | + m |
15 | 36 | }
|
| 37 | + |
| 38 | +#to test you do like this: |
| 39 | +#>mat1 = matrix(r, nrow=10, ncol=10) |
| 40 | +#>x = makeCacheMatrix(mat1) |
| 41 | +#>cacheSolve(x) |
| 42 | +#and you get the answer very fast! Repeat to see message |
| 43 | +#or read in matrix like this: |
| 44 | +#temp= read.csv("testfile.csv") |
| 45 | +#temp1 <- as.matrix(temp) |
| 46 | +#x = makeCacheMatrix(temp1) |
| 47 | +#...etc. but don't forget R assumes header and |
| 48 | +#it will complain matrix not square if u forget!!! |
0 commit comments