File tree 1 file changed +30
-5
lines changed
1 file changed +30
-5
lines changed Original file line number Diff line number Diff line change 1
1
# # Put comments here that give an overall description of what your
2
2
# # functions do
3
3
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 )
6
8
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 )
8
23
}
9
24
10
25
11
26
# # Write a short comment describing this function
27
+ # #This is used to get the cache data
12
28
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'
15
40
}
You can’t perform that action at this time.
0 commit comments