File tree Expand file tree Collapse file tree 1 file changed +28
-8
lines changed Expand file tree Collapse file tree 1 file changed +28
-8
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # below are the functions that cache and compute the inverse of
2
+ # # a matrix
3
3
4
- # # Write a short comment describing this function
5
-
6
- makeCacheMatrix <- function (x = matrix ()) {
4
+ # # This function creates special "matrix" object that can cache its
5
+ # # inverse
7
6
7
+ makeCacheMatrix <- function (mtx = matrix ()) {
8
+ inverse <- NULL
9
+ set <- function (x ) {
10
+ mtx <<- x ;
11
+ inverse <<- NULL ;
12
+ }
13
+ get <- function () return (mtx );
14
+ setinv <- function (inv ) inverse <<- inv ;
15
+ getinv <- function () return (inverse );
16
+ return (list (set = set , get = get , setinv = setinv , getinv = getinv ))
8
17
}
9
18
10
19
11
- # # Write a short comment describing this function
20
+ # # This function computes the inverse of the special "matrix"
21
+ # #returned by makecacheMatrix above. If inverse has been calculated
22
+ # # already, then cacheSolve Should retrieve the inverse the
23
+ # # from the cache
12
24
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
25
+ cacheSolve <- function (mtx , ... ) {
26
+ inverse <- mtx $ getinv()
27
+ if (! is.null(inverse )) {
28
+ message(" Getting cached data..." )
29
+ return (inverse )
30
+ }
31
+ data <- mtx $ get()
32
+ invserse <- solve(data , ... )
33
+ mtx $ setinv(inverse )
34
+ return (inverse )
15
35
}
You can’t perform that action at this time.
0 commit comments