我正在尝试将矩阵相乘。矩阵中的值表示每个循环的不同概率。因此,我使用循环来更新矩阵中的值。一开始它工作得很好,但后来我得到了反馈:下标超出了界限。错误消息显示我的下一个值 [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