【发布时间】:2026-01-25 01:55:01
【问题描述】:
我需要将矩阵estado乘以矩阵matriz_de转换并将变量estado重新分配给乘法的结果,我必须这样做1000次,我需要重复模拟100 次。
即estado矩阵乘以matriz_de_transicion 1000次,看看estado的值是否收敛,然后重复此操作100次。
我尝试过使用 for 循环,但是我将值重新分配给变量 estado 的方式不起作用,因为我一直得到相同的结果,即使我手动执行它似乎也有效。
This is my code:
nombre_estados <- c("Estado 1","Estado 2","Estado 3")
matriz_de_transicion <- matrix(c(0.2,0.7,0.1,
0.3,0.7,0.0,
0.1,0.4,0.5),
byrow = T,nrow = 3, dimnames = list(nombre_estados,nombre_estados))
estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1)
estado <- estado %*% matriz_de_transicion
estado # 0.26 0.67 0.07
estado <- estado %*% matriz_de_transicion
estado # 0.26 0.679 0.061
# repeat this 1000 times
非常感谢您的回答,我怎样才能将这些最终的estados 添加到 100 x 3 矩阵中?我创建了 100 x 3 矩阵:
datos <- matrix(c(0,0,0),byrow = F,ncol = 3, nrow = 100)
然后我尝试做一个嵌套循环:
for(i in 1:100){
for(i in 1:1000){
estado <- estado %*% matriz_de_transicion
}
datos[,1] <- estado[1,1]
datos[,2] <- estado[1,2]
datos[,3] <- estado[1,3]
estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1) # I thought this would reset the value to the initial state but I fill the 100 x 3 matrix with the same values.
}
[,1] [,2] [,3]
[1,] 0.2631579 0.6842105 0.05263158
[2,] 0.2631579 0.6842105 0.05263158
[3,] 0.2631579 0.6842105 0.05263158
[4,] 0.2631579 0.6842105 0.05263158
[5,] 0.2631579 0.6842105 0.05263158
[6,] 0.2631579 0.6842105 0.05263158
[7,] 0.2631579 0.6842105 0.05263158
[8,] 0.2631579 0.6842105 0.05263158
我正在努力重置 estado 变量的值,对于我最终需要获得的 100 个模拟中的每一个,因此 1:1000 循环中的每一个对于 @987654333 都有相同的起始值@。
【问题讨论】: