diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..af5f23d6f9b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,51 @@ -## Put comments here that give an overall description of what your -## functions do +## Two functions here +## makeCacheMatrix sets a matrix into the cache +## cacheSolve returns the inverse of a matrix -## Write a short comment describing this function +## makeCacheMatrix takes in matrix x and stores in cache makeCacheMatrix <- function(x = matrix()) { + i <- NULL + + set <- function(y) { + x <<- y + i <<- NULL + } + + get <- function() x + + setInverse <- function(inverse) { + i <<- inverse + } + getInverse <- function() { + i + } + + list( + set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse + ) } -## Write a short comment describing this function +## Either returns the inverse of the cached matrix for x or returns the matrix of x cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + i <- x$getInverse() + + if(!is.null(i)) { ## getting cache + return(i) + } + + data <- x$get() + + i <- solve(data, ...) + + x$setInverse(i) + + i ## returns the inverse matrix }