Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 马尔可夫矩阵的更新循环:下标越界_R_Loops_Matrix - Fatal编程技术网

R 马尔可夫矩阵的更新循环:下标越界

R 马尔可夫矩阵的更新循环:下标越界,r,loops,matrix,R,Loops,Matrix,我在试着乘矩阵。矩阵中的值表示每个周期不同的概率。因此,我使用loop for来更新矩阵中的值。一开始它工作得很好,但后来我得到了反馈:下标超出了边界。错误消息显示了我的下一个值[4、]210、323、467。为什么它们不显示在底部 > initial_patient_distribtion <- c (1000,0,0) > aaa <- c(1,0.7,0.6,0.5,0.4) > bbb <- c(1, 0.2,0.3, 0.4, 0.5) > c

我在试着乘矩阵。矩阵中的值表示每个周期不同的概率。因此,我使用loop for来更新矩阵中的值。一开始它工作得很好,但后来我得到了反馈:下标超出了边界。错误消息显示了我的下一个值[4、]210、323、467。为什么它们不显示在底部

> initial_patient_distribtion <- c (1000,0,0)
> aaa <- c(1,0.7,0.6,0.5,0.4)
> bbb <- c(1, 0.2,0.3, 0.4, 0.5)
> ccc <- c(1, 0.1,0.1,0.1,0.1)
> 
> cycle_patient_distribution_dasa_no2nd[1,] <-initial_patient_distribtion
>  for (i in 2:length(aaa)){ 
+ trans_matrix_dasa_no2nd <- matrix (,nrow=3,ncol=3)
+ trans_matrix_dasa_no2nd[1,] <- c(aaa[i],bbb[i],ccc[i])
+ trans_matrix_dasa_no2nd[2,] <- c(0,0.5,0.5)
+ trans_matrix_dasa_no2nd[3,] <- c(0,0,1)
+ 
+ cycle_patient_distribution_dasa_no2nd[i,] <- cycle_patient_distribution_dasa_no2nd[i-1,]%*%(trans_matrix_dasa_no2nd)}
Error in `[<-`(`*tmp*`, i, , value = c(210, 323, 467)) : 
  subscript out of bounds
> 
> cycle_patient_distribution_dasa_no2nd
         [,1] [,2] [,3]
[1,] 1000    0    0
[2,]  700  200  100
[3,]  420  310  270
>初始患者分布aaa bbb ccc
>循环患者分布第二[1,]对于(i/2:长度(aaa)){

+trans_matrix_dasa_no2nd你的
for
循环进入
长度(aaa)
(5)并尝试在
i==5
时访问
循环患者分布\u dasa_no2nd[i,]
。但是,你会看到
循环患者分布\u dasa_no2nd[5,]
抛出错误,因为该矩阵的维数是3x3

如果您的代码不符合您的要求,则需要将
for
循环中的结束索引更改为3,或者修改矩阵的维度:

trans_matrix_dasa_no2nd <- matrix (,nrow=length(aaa),ncol=3)

trans_matrix_dasa_no2nd谢谢,如果我更改,那么我将在cycle_patient_distribution_dasa_no2nd[I-1,]%*%(trans_matrix_Dasa2nd)中得到以下错误:不一致参数您正在尝试将1x3矩阵与5x3矩阵相乘。内部尺寸必须相同:1x5 x 5x3或1x3 x 3x3。为便于查找,不需要最后的矩阵相乘步骤。转移概率已应用,因为您从初始状态开始。