From 8ae0e0fa5499a2bb89934e98064cbe5a5c1f6428 Mon Sep 17 00:00:00 2001 From: hectorcm Date: Sun, 22 Nov 2015 17:44:27 -0600 Subject: [PATCH 1/2] Hw2 last version --- cachematrix.R | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..b65c4427f98 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## makeCacheMatrix containes 4 functions set, get, setinverse, getinverse. +## get.-returns de vector x set.- change the vectore stored in the main function just in case of a new valor +## the variable m is used to determinate when the matrix inverse has been already calculated +## getinverse and setinverse are used to put the value in m variable makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## cacheSolve check if the valu of m is empty in that case it calculate the inverse, otherwaze returns the value that has been already calated cacheSolve <- function(x, ...) { + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m ## Return a matrix that is the inverse of 'x' } From 3ad07bf826341c3a351e6a49edeb1284c04cbc87 Mon Sep 17 00:00:00 2001 From: hectorcm Date: Sun, 22 Nov 2015 17:47:52 -0600 Subject: [PATCH 2/2] hw2 Final version --- cachematrix.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index b65c4427f98..e5b62d88857 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,7 @@ ## makeCacheMatrix containes 4 functions set, get, setinverse, getinverse. ## get.-returns de vector x set.- change the vectore stored in the main function just in case of a new valor ## the variable m is used to determinate when the matrix inverse has been already calculated -## getinverse and setinverse are used to put the value in m variable +## getinverse and setinverse are used to put the value in m variable. makeCacheMatrix <- function(x = matrix()) { m <- NULL @@ -16,7 +16,7 @@ makeCacheMatrix <- function(x = matrix()) { setinverse <- function(solve) m <<- solve getinverse <- function() m list(set = set, get = get, - setinverse = setinverse, + setinverse = setinverse, getinverse = getinverse) }