Skip to content

Commit f8c2ed3

Browse files
cachematrix.R added
1 parent 7f657dd commit f8c2ed3

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

cachematrix.R

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
31

4-
## Write a short comment describing this function
52

6-
makeCacheMatrix <- function(x = matrix()) {
3+
#The first function,makeCacheMatrix caches the inverse of input matrix and it
4+
#is a list of functions for following tasks:
5+
#1.set the value of matrix
6+
#2.get the value of matrix
7+
#3.set the value of inverse
8+
#4.get the value of inverse
9+
10+
#The second function cacheSolve computes the value of inverse of matrix
11+
#and if its already computed then it returns the cached value.
712

13+
14+
##For caching the inverse matrix
15+
makeCacheMatrix <- function(x = matrix()) {
16+
if(!is.matrix(x))
17+
stop("Input should be a matrix.")
18+
x_inv<-NULL
19+
set<-function(y)
20+
{
21+
x<<-y
22+
x_inv<-NULL
23+
}
24+
25+
get<-function() x
26+
set_inv<-function(invMat) x_inv<<-invMat
27+
get_inv<-function() x_inv
28+
list(set=set,
29+
get=get,
30+
set_inv=set_inv,
31+
get_inv=get_inv)
832
}
933

1034

11-
## Write a short comment describing this function
35+
## For computing the inverse of matrix.
1236

1337
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
38+
x_inv<-x$get_inv()
39+
if(!is.null(x_inv))
40+
{
41+
message("getting cached value")
42+
return(x_inv)
43+
}
44+
mat<-x$get()
45+
invMat<-solve(mat)
46+
x$set_inv(invMat)
47+
invMat
48+
1549
}

0 commit comments

Comments
 (0)