diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ce6250418cc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do +## The code descibed below use two functions. The function makeCacheMatrix first calculate the inverse of a matix and save that for caching if same matrix is passed as an argument using cacheSolve funciton. -## Write a short comment describing this function +## This funciton uses solve() function from "base" packages to calculate the inverse. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(solve) m <<- solve + getinv <- function() m + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } - - -## Write a short comment describing this function +## cacheSolve function check first if the matrix inverse is already calculated, else calculates using solve() function. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + m <- x$getinv() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinv(m) + m + ## This method return a matrix calles "m" that is the inverse of 'x' +} \ No newline at end of file