Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
122 lines (87 loc) · 3.43 KB
/
Copy pathcachematrix.R
File metadata and controls
122 lines (87 loc) · 3.43 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
##Programming Assignment 2: Lexical Scoping
##Diego Jaramillo Celis
##Assignment: Caching the Inverse of a Matrix
##**********************************************************************##
## PARA VER COMO PROBAR EL EJERCICIO, POR FAVOR IR AL FINAL DEl ARCHIVO ##
## TO SEE HOW TO TEST THIS EXERCISE, PLEASE GO TO END OF FILE ##
##**********************************************************************##
## NOTA: Ejercicio basado en el ejemplo "Caching the Mean of a Vector" -RPENG/R-PROGRAMMING/COURSERA
## NOTE: Exercise based on example "Caching the Mean of a Vector" -RPENG/R-PROGRAMMING/COURSERA
## makeCacheMatrix: Esta función crea un "matriz" que puede capturar su inversa
## makeCacheMatrix: This function creates "matrix" that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
# defino la variable "invx" para almacenar el valor de la matriz inversa
# define "invx" variable to store the inverse matrix value
invx <- matrix()
#Seteo de "x" en el entorno padre y limpieza de la variable invx
#Set of "x" in parent environment and clean invx variable
set <- function(usrmat=matrix()) {
x <<- usrmat
invx <<- NULL
}
#Obtener el valor the la matriz
#Get the value of the matrix
get <- function() x
#Seteo del valor de la matriz inversa
#Set the inverse matrix value
setinvrs <- function(solve) invx <<- solve
#Obtener el valor de la matriz inversa
#Get the inverse matrix value
obtenerinv <- function() invx
#Lista de las funciones disponibles
#List of available functions
list(set = set, get = get,setinvrs=setinvrs,obtenerinv=obtenerinv)
}
## cachesolve: Retorna una matriz que es la inversa de la matriz "x"
## cachesolve: Return a matrix that is the inverse of "x"
cacheSolve <- function(x, ...) {
# Verificar si ya existe una matriz inversa
# Verify if there is an inverse matrix already
invx <- x$obtenerinv()
if(!is.null(invx)) {
message("getting cached inverse")
return(invx)
}
#Obtener la matriz definida en la función makeCacheMatrix
#Get matrix defined in makeCacheMatrix function
mat <- x$get()
#Generar la matriz inversa
#Generate the inverse matrix
invx <- solve(mat, ...)
x$setinvrs(invx)
#muestra la matriz inversa
#show inverse matrix
invx
}
## PRUEBA DE CACHEMATRIX
## TEST OF CACHEMATRIX
#Ejecute estos pasos para probar las funciones
#Execute this steps to test the functions
#Definir valor para X, por ejemplo:
#Define value to X, i.e:
# x <- matrix(c(2,3,2,1,2,1,1,1,2), nrow=3, ncol=3)
# x <- matrix(c(2,3,2,1), nrow=2, ncol=2)
#Verificar que "x" sea una matriz
#Verify that "x" is matrix
#class(x)
#Asignar el resultado de la función "makeCacheMatrix(x)" a la variable "result"
#Assign the result of "makeCacheMatrix(x)" function to the variable "result"
#result <- makeCacheMatrix(x)
#Obtener el valor de la matriz
#Get the matrix value
#result$get()
#matriz<-result$get()
#Generar la matriz inversa
#Generate the inverse matrix
#result$setinvrs(solve(x))
#Obtener la matriz inversa
#Get the inverse matrix
#result$obtenerinv()
#inversa<-result$obtenerinv()
#El resultado es correcto cuando la multiplicación de la matrices...
#... es igual a la matriz indentidad (matriz*inversa=identidad)
#The result is correct when the product of both matrix is equal to identity matrix (matrix*inverse=identity)
##PUT THIS IN CONSOLE:
#matriz
#inversa
#round((matriz %*% inversa), digits = 1)