From e7e2c5dbe5e3ecd034dd809bbf15dcf46a346d53 Mon Sep 17 00:00:00 2001 From: MLawniczak Date: Sat, 19 Apr 2014 15:15:28 -0400 Subject: [PATCH] final --- cachematrix.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2d6130ad8de 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,13 +3,51 @@ ## Write a short comment describing this function +## This function created a special matrix object that +## can cache its inverse + makeCacheMatrix <- function(x = matrix()) { + + m <- NULL + + set <- function(y) { + x <<- y + m <<- NULL + } + + get <- function() x + + setinverse <- function(solve) m <<- solve + + getinverse <- function() m + + list( set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } ## Write a short comment describing this function +## This function computes the inverse of the special matrix +## returned by makeCacheMatrix. If the inverse has already +## been calculated (and the matrix has not changed) then +## cacheSolve should retrieve the inverse from the cache + cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + m <- x$getinverse() + + if(!is.null(m)) { + message("getting cached data") + return(m) + } + + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m + }