File tree Expand file tree Collapse file tree 1 file changed +49
-7
lines changed Expand file tree Collapse file tree 1 file changed +49
-7
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
+ # # Create a cacheable inverse matrix
2
+ makeCacheMatrix <- function ( x = matrix () ) {
3
3
4
- # # Write a short comment describing this function
4
+ # # initialize the property
5
+ i <- NULL
5
6
6
- makeCacheMatrix <- function (x = matrix ()) {
7
+ # # get the matrix
8
+ get <- function () {
9
+ x # # Return matrix
10
+ }
7
11
12
+ # # set the matrix
13
+ set <- function ( m ) {
14
+ x <<- m
15
+ i <<- NULL
16
+ }
17
+
18
+ # # Method to get the inverse of the matrix
19
+ getInv <- function () {
20
+ # # Return the inverse property
21
+ i
22
+ }
23
+
24
+ # # set inverse matrix
25
+ setInv <- function ( tmp ) {
26
+ i <<- tmp
27
+ }
28
+
29
+ # # return a list methods
30
+ list (set = set , get = get , setInv = setInv , getInv = getInv )
8
31
}
9
32
10
33
11
- # # Write a short comment describing this function
34
+ # # Get the inverse matrix, and use cache if it has been already calculated
35
+ cacheSolve <- function ( x , ... ) {
36
+
37
+ # # Return inverse of x
38
+ m <- x $ getInv()
39
+
40
+ # # return the inverse if its already set
41
+ if ( ! is.null( m ) ) {
42
+ message( " cached value" )
43
+ return (m )
44
+ }
45
+
46
+ # # Get matrix
47
+ data <- x $ get()
48
+
49
+ # # Calculate the inverse
50
+ m <- solve( data ) %*% data
51
+
52
+ # # Set inverse
53
+ x $ setInv( m )
12
54
13
- cacheSolve <- function ( x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
55
+ # # Return matrix
56
+ m
15
57
}
You can’t perform that action at this time.
0 commit comments