diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..33996618c42 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +## makeCacheMatrix creates a function that stores a matrix for use as an argument +## to cacheSolve cacheSolve finds the inverse of a matrix created with +## makeCacheMatrix + +## makeCacheMatrix returns a function with a reference to the matrix passed as an +## argument The return value of this function will be used as an argument to +## cacheSolve +makeCacheMatrix <- function(x = matrix()) { + m <- NULL # Set return value to NULL + # Functions to get and set the matrix + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv = function(inverse) m <<- inverse + getinv = function() m + list(set = set, get = get, setinv = setinv, getinv = getinv) +} + +## cacheSolve finds the inverse of a matrix created with makeCacheMatrix If the +## inverse has already been found it is returned from a cache Else it is +## calculated and then returned. +cacheSolve <- function(x, ...) { + m = x$getinv() + # If not null we already calculated the inverse so return it + if (!is.null(m)) { + return(m) + } + # m was not null so find inverse + data <- x$get() + m <- solve(data, ...) + x$setinv(m) + return(m) +}