From c6d5e843c4127a66ac7a9924871553e64fccef78 Mon Sep 17 00:00:00 2001 From: cginer Date: Mon, 28 Apr 2014 09:00:34 +0200 Subject: [PATCH 1/2] assignment --- cachematrix.R | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..71fb972f5f4 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,45 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function - +## Write a short comment describing this function: +## Here we define the function to create an object that contains +## information about a matrix and also its inverse. This object +## contains 4 functions: "set" that it's used when creating the +## object; "get" that allows you to retrieve the simple matrix; +## "setinverse" that can store the value of its inverse; and +## "getinverse" that allows you to retrieve the inverse if it +## has been calculated (if not it is set to NULL); makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y){ + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(solve) i <<- solve + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## Write a short comment describing this function: +## cacheSolve calculates the inverse of an object of those +## created by makeCacheMatrix. If the inverse of the object +## has never been calculated yet, it applies solve to the matrix +## and store the inverse in the object (via "setinverse"). If the +## inverse has been obtained before, it retrieves it from the +## stored value in the object (via "getinverse"). cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + i <- x$getinverse() + if (!is.null(i)){ + message("getting cached data!") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i +} \ No newline at end of file From aeeb82a0ce9079545316197157c47024041de277 Mon Sep 17 00:00:00 2001 From: cginer Date: Mon, 28 Apr 2014 09:16:42 +0200 Subject: [PATCH 2/2] update assignment --- cachematrix.R | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 71fb972f5f4..f0d5864c944 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,18 @@ ## Put comments here that give an overall description of what your -## functions do +## functions do: +## With this two functions we can create a special matrix object that +## contains the matrix and also its inverse when it has been calculated +## the first time. This is useful to avoid computing many times the same +## value with a potentialy time-consuming process. ## Write a short comment describing this function: -## Here we define the function to create an object that contains -## information about a matrix and also its inverse. This object -## contains 4 functions: "set" that it's used when creating the -## object; "get" that allows you to retrieve the simple matrix; -## "setinverse" that can store the value of its inverse; and -## "getinverse" that allows you to retrieve the inverse if it -## has been calculated (if not it is set to NULL); +## "makeCacheMatrix" creates an object that contains information about a +## matrix and also its inverse. This object contains 4 functions: "set" +## that it's used when creating the object; "get" that allows you to +## retrieve the simple matrix; "setinverse" that can store the value of +## its inverse; and "getinverse" that allows you to retrieve the inverse +## if it has been calculated (if not it is set to NULL); + makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y){