diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..8ded3207f23 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,72 @@ ## Put comments here that give an overall description of what your -## functions do +## functions do + ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +library(MASS) + +makeCachematrix <- function(x=matrix()) + +{ + + inv<-NULL #initializing inverse as null + + set<-function(y) + + { + + x<<-y + + inv<<-NULL + + } + + get<-function()x #function to get matrix + + setinv<-function(inverse)inv<<-inverse + + getinv<-function() + + { + + inver<-ginv(x) + + inver%*%x #function to obtain inverse of matrix + + } + + list(set =set, get=get, + + setinv=setinv, + + getinv=getinv) } +cachesolve <- function(x,...) ##gets cache data -## Write a short comment describing this function +{ + + inv<-x$getinv() + + if(!is.null(inv)) ##checking in inverse is null + + { + + message("getting cached data!") + + return(inv) + + } + + data<-x$get() + + inv<-solve(data....) ##calculates inverse value + + x$setinv(inv) + + inv -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' } +