Skip to content

Commit b76dbb6

Browse files
Update cachematrix.R
1 parent 7f657dd commit b76dbb6

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

cachematrix.R

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
5-
4+
##There are two functions makeCacheMatrix,makeCacheMatrix
5+
##makeCacheMatrix consists of set,get,setinv, getinv
6+
##library(MASS) is used to calculate inverse for non squared as well as square matrices
7+
library(MASS)
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
inv<-NULL #initializing inverse as NULL
10+
set<-function(y){
11+
x<<-y
12+
inv<<-NULL
13+
}
14+
get<-function()x #function to get matrix x
15+
setinv<-function(inverse)inv<<-inverse
16+
getinv<-function(){
17+
inver<-ginv(x)
18+
inver%*%x #function to obtain inverse of the matrix
19+
}
20+
list(set = set, get = get,
21+
setinv = setinv,
22+
getinv = getinv)
823
}
924

1025

1126
## Write a short comment describing this function
27+
##This is used to get the cache data
1228

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
29+
cacheSolve <- function(x, ...) ##gets cache data
30+
{
31+
inv<-x$getinv()
32+
if(!is.null(inv)){ #checking whether inverse is NUll
33+
message("getting cached data!")
34+
return(inv) #returns inverse value
35+
}
36+
data<-x$get()
37+
inv<-solve(data,...) #calculates inverse value
38+
x$setinv(inv)
39+
inv ## Return a matrix that is the inverse of 'x'
1540
}

0 commit comments

Comments
 (0)