From 6a0ba3c9e6c091ee15e4cc18f32b6fa21bf765ce Mon Sep 17 00:00:00 2001 From: kranthi Date: Thu, 23 Apr 2015 12:38:34 -0700 Subject: [PATCH] inverse cache matrix computation --- cachematrix.R | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..7d575140ee6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do +## Goal of the functions is to cache the inverse of the matrix over repeatedly computing the inverse. -## Write a short comment describing this function +## makeCacheMatrix caches the inverse of matrix. makeCacheMatrix <- function(x = matrix()) { - + i <- null + set <- function(y){ + x <<- y + i <<- null + } + get <- function() x + setinv <- function(inv) i <<- inv + getinv <- function() i + list(set = set, get = get, setinv = setinv, getinv = getinv) + } -## Write a short comment describing this function - +## cacheSolve computes the inverse of the matrix retruned by makeCacheMatrix. If the inverse is +## already calculated then its returns the cached inverse value cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinv() + if(!is.null(i)){ + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinv(i) + i }