|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Function returns the inverse of a square matrix. It does not check that the matrix |
| 2 | +## is invertible. |
| 3 | +## The function first calls the cache function to see if the result has already been |
| 4 | +## computed and either returns the cache or computes the inverse |
| 5 | +## The function call is: cacheSolve(makeCacheMatrix(x)) |
| 6 | +## where x is the invertible matrix. |
3 | 7 |
|
4 | 8 | ## Write a short comment describing this function
|
5 | 9 |
|
6 | 10 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 11 | + m <- NULL # initializes m |
| 12 | + set <- function(y) { |
| 13 | + x <<- y # x survives the end of the function to pass to next |
| 14 | + m <<- NULL # resets m to NULL and survives the end of the function |
| 15 | + } |
| 16 | + get <- function() x # returns just x |
| 17 | + setinverse <- function(solve) m <<- solve #the computed value is set into the list here |
| 18 | + getinverse <- function() m |
| 19 | + list(set = set, get = get, |
| 20 | + setinverse = setinverse, |
| 21 | + getinverse = getinverse) |
8 | 22 | }
|
9 | 23 |
|
10 | 24 |
|
11 | 25 | ## Write a short comment describing this function
|
12 | 26 |
|
13 | 27 | cacheSolve <- function(x, ...) {
|
14 | 28 | ## Return a matrix that is the inverse of 'x'
|
| 29 | + m <- x$getinverse() #gets the matrix m from above and tests if it's NULL |
| 30 | + if(!is.null(m)) { #if not NULL, returns its value |
| 31 | + message("getting cached data") |
| 32 | + return(m) |
| 33 | + } |
| 34 | + data <- x$get() #just gets x from above |
| 35 | + m <- solve(data, ...) #compute here |
| 36 | + x$setinverse(m) #sets the cache in the function setinverse() above |
| 37 | + m |
15 | 38 | }
|
0 commit comments