Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
60 lines (51 loc) · 2.03 KB
/
Copy pathcachematrix.R
File metadata and controls
60 lines (51 loc) · 2.03 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## This pair of functions can be used to calculate the inverse of a Matrix
## 1- Create an object this way: TempObject = makeCacheMatrix ( YourData )
## 2- Calculate the inverse matrix : InverseM = cachesolve(TempObject)
## 3- For changing the data, execute: TempObject$set( NewData )
## 4- Calculate the inverse matrix : InverseM = cachesolve(TempObject)
## --------------------- makeCacheMatrix --------------------------------
# This first function creates a special "matrix" object that can cache
# its inverse
# makeCacheMatrix function builds a list of functions
# with two internal variables:
# matr : the data matrix
# minv : the inverse of the data matrix. Starts like NULL and becomes NULL
# when MATR changes, with the SET function
makeCacheMatrix <- function( matr = matrix() ) {
minv <- NULL
set <- function(y) {
matr <<- y
minv <<- NULL
}
get <- function() matr
setminv <- function(minv_new) minv <<- minv_new
getminv <- function() minv
list(set = set, get = get,
setminv = setminv,
getminv = getminv)
}
## ------------------------ cachesolve ------------------------------
# This second function computes the inverse of the special "matrix"
# returned by makeCacheMatrix above.
# If the inverse has already been calculated (and the matrix
# has not changed), then the cachesolve should retrieve the
# inverse from the cache.
# cachesolve function calculates de inverse matrix, if it doesn't exist.
# If exists, returns the last value, without calculating it again
cachesolve <- function(matrli , ... ) {
minv <- matrli$getminv()
if(!is.null(minv) ) {
message("Inverse matrix hasn't changed. Getting cached data")
return(minv)
}
data <- matrli$get()
minv <- solve(data , ... )
matrli$setminv(minv)
minv
}
## -------------------------- Tests ------------------------------------
# Tests
# matx = makeCacheMatrix ( matrix(rnorm(4) ,2 , 2) )
# matx = makeCacheMatrix ( )
# matx$set(matrix(rnorm(4) ,2 , 2) )
# cachesolve(matx)