diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..62616116cbf 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,44 @@ -## Put comments here that give an overall description of what your -## functions do +## Below are two functions that are used to create a special object that stores +## a numeric matrix and caches its inversion -## Write a short comment describing this function +## The first function, makeCacheMatrix creates a special "matrix" +## which is really a list containing a function to -makeCacheMatrix <- function(x = matrix()) { +## 1. set the value of the matrix +## 2. get the value of the matrix +## 3. set the value of the inversion +## 4. get the value of the inversion +makeCacheMatrix <- function(x = matrix()) +{ + inv <- NULL + set <- function(y) + { + x <<- y + inv <<- NULL + } + get <- function() x + setinv <- function(i) inv <<- i + getinv <- function() inv + list(set = set, get = get, setinv = setinv, getinv = getinv) } +## The following function calculates the inversion of the special "matrix" +## created with the makeCacheMatrix function. It first checks if the inversion +## has already been calculated. If so, it gets the inversion from the cache and +## skips the computation. Otherwise, it calculates the inversion of the data +## and sets the value of the inversion in the cache via the setinv function. -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(x, ...) +{ + inv <- x$getinv() + if (!is.null(inv)) + { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv }