Skip to content

Commit bc6a240

Browse files
committed
- Adding the functions for the assignment
1 parent 7f657dd commit bc6a240

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

cachematrix.R

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,46 @@
33

44
## Write a short comment describing this function
55

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
711

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 )
829
}
930

1031

1132
## Write a short comment describing this function
1233

34+
## Return a matrix that is the inverse of 'x'
1335
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
1548
}

0 commit comments

Comments
 (0)