File tree Expand file tree Collapse file tree 1 file changed +35
-2
lines changed Expand file tree Collapse file tree 1 file changed +35
-2
lines changed Original file line number Diff line number Diff line change 3
3
4
4
# # Write a short comment describing this function
5
5
6
- makeCacheMatrix <- function (x = matrix ()) {
6
+ # This function creates a list containing a function to
7
+ # set the value of the matrix
8
+ # get the value of the matrix
9
+ # set the value of the inverse of the matrix
10
+ # get the value of the inverse of the matrix
7
11
12
+ makeCacheMatrix <- function (x = matrix ()) {
13
+ # minv will store the matrix
14
+ minv <- NULL
15
+ # setter for minv
16
+ set <- function (y ) {
17
+ x <<- y
18
+ m <<- NULL
19
+ }
20
+ # getter for minv
21
+ get <- function () x
22
+ # setter for inverse matrix
23
+ setinv <- function (inverse ) minv <<- inverse
24
+ # getter for inverse matrix
25
+ getinv <- function () minv
26
+
27
+ # return the list
28
+ list ( set = set , get = get , setinv = setinv , getinv = getinv )
8
29
}
9
30
10
31
11
32
# # Write a short comment describing this function
12
33
34
+ # # Return a matrix that is the inverse of 'x'
13
35
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
36
+ # get the inverse of the matrix
37
+ minv <- x $ getinv()
38
+ # if the inverse is already calculated, return it
39
+ if (! is.null(minv )) {
40
+ message(" getting cached data .." )
41
+ return (minv )
42
+ }
43
+ # calculate the inverse when is not yet calculated
44
+ data <- x $ get()
45
+ minv <- solve(data )
46
+ x $ setinv(minv )
47
+ minv
15
48
}
You can’t perform that action at this time.
0 commit comments