From 0574b7778c908864754f5af69b23fb54d0268e81 Mon Sep 17 00:00:00 2001 From: David Boeke Date: Sun, 21 Sep 2014 14:06:12 -0400 Subject: [PATCH] Finished Prog Assignment 2 --- cachematrix.R | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..52f35aeb5ca 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Coursera R Programming Course (rprog-007) +## Programming Assignment 2 (Week 3) +## Set of functions that caches the inversion of a matrix +## makeCacheMatrix is function creates a special "matrix" object that can +## cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + ## Return a vector that is a cache of an inverse matrix + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(solve) m <<- solve + getmatrix <- function() m + list(set=set, get=get, setmatrix=setmatrix, getmatrix=getmatrix) } -## Write a short comment describing this function - +## This function computes the inverse of the special "matrix" returned by +## makeCacheMatrix above. If the inverse has already been calculated (and the +## matrix has not changed), then the cachesolve should retrieve the inverse +## from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getmatrix() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setmatrix(m) + m }