From 2762b71076027a89c3345778dcdd8405f3b9da6a Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 21 Jun 2014 11:29:52 -0400 Subject: [PATCH] Caching the Inverse of a Matrix functions --- cachematrix.R | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..86a8a6b80b2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## The matrix inversion is usualy a costly operation. +## The set of functions below intended to cache matrix inverse +## to save computational time on inverse calculartion. +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinv <- function(inverse) i <<- inverse + getinv <- function() i + list(set = set, get = get, setinv = setinv, getinv = getinv) } -## 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, 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' + i <- x$getinv() + if (!is.null(i)) { + return(i) + } + data <- x$get() + inverse <- solve(data, ...) + x$setinv(inverse) + inverse }