File tree Expand file tree Collapse file tree 1 file changed +25
-6
lines changed Expand file tree Collapse file tree 1 file changed +25
-6
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
+ # # Function to cache the inverse of input matrix x
6
5
makeCacheMatrix <- function (x = matrix ()) {
7
-
6
+ inv <- NULL
7
+ set <- function (y ) {
8
+ x <<- y
9
+ inv <<- NULL
10
+ }
11
+ get <- function () x
12
+ setInv <- function (m_inv ) inv <<- m_inv
13
+ getInv <- function () inv
14
+ list (set = set , get = get ,
15
+ setInv = setInv ,
16
+ getInv = getInv )
8
17
}
9
18
10
19
11
- # # Write a short comment describing this function
12
-
20
+ # # Find the inverse of the input matrix x.
21
+ # # If it is not found in cache, find the inverse for the first time & return it.
22
+ # # Else return it from the cache itself.
23
+ # # Return a matrix that is the inverse of 'x'
13
24
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
25
+ inv <- x $ getInv()
26
+ if (! is.null(inv )) {
27
+ message(" getting cached data" )
28
+ return (inv )
29
+ }
30
+ data <- x $ get()
31
+ inv <- solve(data )
32
+ x $ setInv(inv )
33
+ inv
15
34
}
You can’t perform that action at this time.
0 commit comments