forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
39 lines (34 loc) · 1.25 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## The function makeCacheMatrix receives a matrix x as parameter and it creates an
## attribute inverse as null, which will contain the inverse of the matrix once it is set by the setInverse function
makeCacheMatrix <- function(x = matrix()) {
## The inverse of the matrix is initially set to null
inverse <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
## Save the inverse Matrix in the variable inverse
setInverse <- function(inverseM) inverse <<- inverseM
getInverse <- function() inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The cacheSolve function return the inverse of a matrix x that receives as a parameter.
## If the inverse is already calculated, then it return just the value that is already store
## Otherwise it calculate the inverse of the matrix
cacheSolve <- function(x, ...) {
##get the value of the inverse Matrix of X
inverse <-x$getInverse()
##if X already have an inverse. It is shown to the user. Otherwise the inverse is calculated
if(!is.null(inverse)){
message("getting cached data")
return(inverse)
}
data <- x$get()
##calculate the inverse of the matrix
inverse <- solve(data,...)
x$setInverse(inverse)
inverse
}