From abe418b5b922580277c5d147cfa93f09d55186a9 Mon Sep 17 00:00:00 2001 From: tiveron Date: Sun, 22 Jun 2014 21:19:10 -0300 Subject: [PATCH] adding assignment --- cachematrix.R | 56 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..09f60d79adc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,57 @@ -## Put comments here that give an overall description of what your -## functions do +## Create a cacheable inverse matrix +makeCacheMatrix <- function( x = matrix() ) { -## Write a short comment describing this function + ## initialize the property + i <- NULL -makeCacheMatrix <- function(x = matrix()) { + ## get the matrix + get <- function() { + x ## Return matrix + } + ## set the matrix + set <- function( m ) { + x <<- m + i <<- NULL + } + + ## Method to get the inverse of the matrix + getInv <- function() { + ## Return the inverse property + i + } + + ## set inverse matrix + setInv <- function( tmp ) { + i <<- tmp + } + + ## return a list methods + list(set = set, get = get, setInv = setInv, getInv = getInv) } -## Write a short comment describing this function +## Get the inverse matrix, and use cache if it has been already calculated +cacheSolve <- function( x, ... ) { + + ## Return inverse of x + m <- x$getInv() + + ## return the inverse if its already set + if( !is.null( m ) ) { + message( "cached value" ) + return(m) + } + + ## Get matrix + data <- x$get() + + ## Calculate the inverse + m <- solve( data ) %*% data + + ## Set inverse + x$setInv( m ) -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return matrix + m }