From cc46adaff4224cb2b7982900414d84bd8f24395e Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Tue, 22 Apr 2014 16:45:39 -0500 Subject: [PATCH 1/2] week3 --- cachematrix.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a8d4719370e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,17 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) } @@ -12,4 +22,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m } From 823693aaf2448c99902d5ec0ee9fdf122cde9606 Mon Sep 17 00:00:00 2001 From: Patricio Diaz Date: Sun, 27 Apr 2014 11:50:42 -0500 Subject: [PATCH 2/2] Update cachematrix.R --- cachematrix.R | 76 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a8d4719370e..5c22b0c6fda 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,34 +1,80 @@ -## Put comments here that give an overall description of what your -## functions do +# R PROGRAMMING +# PEER ASSESSMENT -## Write a short comment describing this function +## Assignment: Caching the Inverse of a Matrix +## Matrix inversion is usually a costly computation +## and their may be some benefit to caching the inverse of a matrix +## rather than compute it repeatedly (there are also alternatives to matrix inversion +## that we will not discuss here). (Coursera Page) -makeCacheMatrix <- function(x = matrix()) { +## This assignment is to write a pair of functions that cache the inverse of a matrix. + +## My function follows the rules described in the instruction for this assignment. I have two +## matrix function on called makeCachematrix and the other called cacheSolve + +############################## +### Make Cache Fuction ### +############################## + +# This function uses the "<<-" operator which is used to assign a value to an object in an environment that is different from the current environment + +makeCacheMatrix <- function(x = matrix()) { # I take an argument x of type matrix m <- NULL set <- function(y) { x <<- y m <<- NULL } get <- function() x - setmean <- function(mean) m <<- mean - getmean <- function() m - list(set = set, get = get, - setmean = setmean, - getmean = getmean) + inverse <- function(solve) m <<- solve + getinverse <- function() m + list(set=set, get = get, + inverse = inverse, + getinverse = getinverse) # I get a list with four fuctions } +################################################################################# +############################## +### Cache Solve Fuction ### +############################## + +# The input for this function is the "special matrix" made from makeCacheMatrix +# The result is the inverse of the matrix coming whether from the special matrix's cache or computation. +# https://class.coursera.org/rprog-002/forum/thread?thread_id=696 -## Write a short comment describing this function cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' - m <- x$getmean() - if(!is.null(m)) { + m <- x$getinverse() #takes the funtion getinverse and send it to m + if(!is.null(m)) { #check if there is the inverse of the matrix message("getting cached data") return(m) } - data <- x$get() - m <- mean(data, ...) - x$setmean(m) + data <- x$get() # get the matrix and send it to data + m <- solve(data, ...) #finds the inverse of the matrix + x$inverse(m) # shows the inverse of the matrix m } +################################################################################# + + +###################################### +# EXAMPLE TO USE WITH cachematrix +# (from: https://class.coursera.org/rprog-002/forum/thread?thread_id=696) + +mymatrix <- makeCacheMatrix(matrix(1:4,2)) + +# this is a "special matrix" using makeCacheMatrix function, I get: + +mymatrix$get() +mymatrix$getinverse() + +mymatrix$set(matrix(5:8,2)) + +cacheSolve(mymatrix) # I get the inverse of the matrix +cacheSolve(mymatrix) + +mymatrix$getinverse() #show the inverse of the matrix + +# test inverse correctness +matrix2 = mymatrix$getinverse() +mymatrix$get() %*% matrix2 #i get the identity matrix