diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cachematrix.R b/cachematrix.R index a50be65aa4..e7d4e405b7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do +## Functions that cache the inverse of a matrix -## Write a short comment describing this function +## First function to create a "matrix" that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + inverse <- NULL + set <- function(x) { + mtrx <<- x; + inverse <<- NULL; + } + get <- function() return(mtrx); + setinv <- function(inv) inverse <<- inv; + getinv <- function() return(inverse); + return(list(set = set, get = get, setinv = setinv, getinv = getinv)) } -## Write a short comment describing this function +## Second function to Solve and print matrix that is the inverse of 'x' cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + inverse <- mtrx$getinv() + if(!is.null(inverse)) { + message("Getting cached data...") + return(inverse) + } + cdata <- mtrx$get() + inverse <- solve(cdata, ...) + mtrx$setinv(inverse) + return(inverse) }