diff --git a/.cachematrix.R.swp b/.cachematrix.R.swp new file mode 100644 index 00000000000..999691e2ba8 Binary files /dev/null and b/.cachematrix.R.swp differ diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..64f8ca4b84f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,46 @@ -## Put comments here that give an overall description of what your -## functions do +## The functions below will take an input such as matrix(c(1,2,3,4),2,2) and return the inverse of this matrix. +## They will first check if the inverse is already in the cache and if so return that cache value, if not the matric +## inverse will be solve for and returned -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## This function will take the user input and create matrix of the values that is store in the cache +makeCacheMatrix <- function(x = matrix()) { + ## create m vector + m <- NULL + ## create the values provided as a matrix + set <- function(y) { + x <<- y + m <<- NULL + } + ## return the matrix of the inputs supplied + get <- function() x + ## stores the function solve for use when pulling data from cache + setinverse <- function(solve) m <<- solve + ## this will print the inverse when called + getinverse <- function() m + ## create a list that holds the names of the items you just stored in the cache + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) + } -## Write a short comment describing this function +## This function will check the cache for the inverse of the defined matrix +## If found it will return the inverse from cache and if not it will create the inverse and store it for later cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## check if the inverse of the amtrix already exists in the cache + m <- x$getinverse() + ## if m is not empty (meaning there is something in the cache for inverse of X), pull the value from cache + if(!is.null(m)) { + message("getting cached data") + return(m) + } + ## get the x matrix from the cache and store it in the data variable + data <- x$get() + ## run the solve function from the cache with the solve function + m <- solve(data, ...) + ## store the inverse + x$setinverse(m) + m }