From 2c7685a080c05c002de6865ad478c1d011d7f6a4 Mon Sep 17 00:00:00 2001 From: Eduardo Vaz de Mello Date: Sat, 21 Feb 2015 16:26:27 -0800 Subject: [PATCH] makeCacheMatrix and cacheSolve implemented. --- cachematrix.R | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..340986796f6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +## Takes a square matrix as argument and returns a list with functions +## to solve the inverse and cache it to return it if was already called once; makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) m <<- inverse + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function - +## Takes a CacheMatrix list as argument and invokes the implemented +## getInverse, which returns the inverted matrix, cached if it was already solved +## called once or solves it and caches it for the next call; cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m }