From 63e7e0d505729ad0e8b48e8b9ce95cb5fdbf2a56 Mon Sep 17 00:00:00 2001 From: Michael Wimsatt Date: Sat, 19 Apr 2014 17:35:12 -0400 Subject: [PATCH] I think this is right. --- cachematrix.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..8ff56a04f99 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do +## This file includes functions for calculating and caching the inversion of a +## matrix. -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## This function creates a matrix which can cache its own inverse. +makeCacheMatrix <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) m <<- inverse + 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 a matrix or retrieves the cached +## value if the matrix hasn't changed. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of '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) + print(m) }