From 0552541ced7c9d2390cc741a16b57c823fe07aa1 Mon Sep 17 00:00:00 2001 From: JohnGitHub886 <44353418+JohnGitHub886@users.noreply.github.com> Date: Tue, 23 Oct 2018 09:25:10 +0800 Subject: [PATCH] Complete two functions. Complete the two function following the detail description which make the matrix inverse. --- cachematrix.R | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..fc0e38d7ea2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,18 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setInverse <- function(inverse) inv <<- inverse + getInverse <- function() inv + list(set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse) } @@ -12,4 +23,14 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + inv <- x$getInverse() + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + mat <- x$get() + inv <- solve(mat, ...) + x$setInverse(inv) + inv }