diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a76df1469db 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 - +## Creates a special matrix which can be used to cache the inverse of this matrix makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + #When setting a new matrix, the inverse needs to be cleared as well + x <<- y + i <<- NULL + } + + get <- function() x + setInverse <- function(inverse) i <<- inverse + getInverse <- function() i + + list(set = set, + get=get, + getInverse=getInverse, + setInverse=setInverse) } - -## Write a short comment describing this function - +## Returns a matrix that is the inverse of x. The result is cached so subsequent calls to this function +## returns the same result without computing it again. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getInverse() + + if(is.null(i)) { + #We need to do the computation + i <- solve(x$get()) + x$setInverse(i) + } + + i }