From 79dab4248769c86a616581f848baf6d443d6e45f Mon Sep 17 00:00:00 2001 From: Jonahlyn Gilstrap Date: Sat, 24 May 2014 20:48:47 -0600 Subject: [PATCH] Programming Assignment 2 submission. --- cachematrix.R | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..bc1d82f0e06 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Functions create and cache the inverse of a matrix. +## Creates an object to hold a matrix and its inverse makeCacheMatrix <- function(x = matrix()) { - + # Holds the inverse matrix + i <- NULL + + # Sets the original matrix and initializes the inverse to NULL + set <- function(y) { + x <<- y + i <<- NULL + } + + # Returns the original matrix + get <- function() x + + # Sets the inverse matrix + setinverse <- function(inverse) i <<- inverse + + # Gets the inverse matrix + getinverse <- function() i + + # Returns a list holding the above functions + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } -## Write a short comment describing this function - +## Gets an inverse matrix from cache or created and caches an inverse matrix. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Attempt to get a cached inverse from the matrix container object + i <- x$getinverse() + if(!is.null(i)){ + message("getting cached inverse") + return(i) + } + # A cached inverse was not found, make one, cache it and return it. + data <- x$get() + i <- solve(data) + x$setinverse(i) + i }