Skip to content

Commit abe418b

Browse files
committed
adding assignment
1 parent 7f657dd commit abe418b

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

cachematrix.R

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
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() ) {
33

4-
## Write a short comment describing this function
4+
## initialize the property
5+
i <- NULL
56

6-
makeCacheMatrix <- function(x = matrix()) {
7+
## get the matrix
8+
get <- function() {
9+
x ## Return matrix
10+
}
711

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)
831
}
932

1033

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 )
1254

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
55+
## Return matrix
56+
m
1557
}

0 commit comments

Comments
 (0)