From 8711646da7a5e684facfc405978bc7eb20fdacae Mon Sep 17 00:00:00 2001 From: Mimi Soo-Hoo Date: Tue, 22 Apr 2014 14:07:48 -0400 Subject: [PATCH 1/2] R Programming Peer Assessment submission --- cachematrix.R | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..970576194d9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do +## The two functions coded below allow a matrix object and its inverse to be +## cached. -## Write a short comment describing this function +## The makeCacheMatrix function takes a given matrix and enables it to +## cache its own inverse. makeCacheMatrix <- function(x = matrix()) { + m <- NULL # creates an empty variable "m" that acts as a cache + set <- function(y) { # set() allows the user to assign x to a new matrix object, y + x <<- y # content of y is assigned to x + m <<- NULL # stores matrix object into the cache + } + get <- function() x # returns the content of the matrix, x + setinverse <- function(solve) m <<- solve # calculates the inverted matrix and stores it in "m" + getinverse <- function() m # identifies the content of the inverted matrix supplied by "m" + list(set = set, get = get, # wraps up all 4 functions into a list that is + setinverse = setinverse, # passed through when makeCacheMatrix is called + getinverse = getinverse) } - -## Write a short comment describing this function +## The cacheSolve function computes the inverse of the matrix object returned by +## makeCacheMatrix above. If the inverse is already calculated and stored in, +## the cache "m", then cacheSolve retrieves the inverse from the cache. If the +## inverse is not found, then cacheSolve calculates the inverse and stores the +## result into the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() # searches for x's inverted matrix compuation in the cache + if(!is.null(m)) { # if x's inverse is found in the cache + message("getting cached data") + return(m) # the cache for x is returned + } # if x is not found in the cache + data <- x$get() # the inverse of x is computed on the fly + m <- solve(data, ...) + x$setinverse(m) # the result is stored back in the cache + m # returns the matrix inversion result + } + + From f918a626948fd6eb59c7c2995a14275556776e07 Mon Sep 17 00:00:00 2001 From: Mimi Soo-Hoo Date: Tue, 22 Apr 2014 14:14:10 -0400 Subject: [PATCH 2/2] R Programming Peer Assessment submission --- cachematrix.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 970576194d9..a55dd5f9003 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -20,13 +20,13 @@ makeCacheMatrix <- function(x = matrix()) { } ## The cacheSolve function computes the inverse of the matrix object returned by -## makeCacheMatrix above. If the inverse is already calculated and stored in, +## makeCacheMatrix above. If the inverse is already calculated and stored in ## the cache "m", then cacheSolve retrieves the inverse from the cache. If the ## inverse is not found, then cacheSolve calculates the inverse and stores the ## result into the cache. cacheSolve <- function(x, ...) { - m <- x$getinverse() # searches for x's inverted matrix compuation in the cache + m <- x$getinverse() # searches for x's inverted matrix computation in the cache if(!is.null(m)) { # if x's inverse is found in the cache message("getting cached data") return(m) # the cache for x is returned