Index in position 2 exceeds array bounds (must not exceed 1)
3 次查看(过去 30 天)
显示 更早的评论
I get error in the title when running this code. Problem lies in a nested for loop defined here. I don't understand this error because it seems to be saying that variables psi_mk and thetam_ss have only one column which isn't true.
for j = 1:nk
for k = 1:ng
sum_ss(j,k) = thetam_ss(j,1)*psi_mk(1,k) + thetam_ss(j,2)*psi_mk(2,k) + thetam_ss(j,3)*psi_mk(3,k) + thetam_ss(j,4)*psi_mk(4,k) + thetam_ss(j,5)*psi_mk(5,k) + thetam_ss(j,6)*psi_mk(6,k) + thetam_ss(j,7)*psi_mk(7,k);
lngammma_mss(j,k) = q(k)*(1 - log(sum_ss(j,k)) - thetam_ss(j,1)*psi_mk(k,1)/sum_ss(j,1) - thetam_ss(j,2)*psi_mk(k,2)/sum_ss(j,2) - thetam_ss(j,3)*psi_mk(k,3)/sum_ss(j,3) - thetam_ss(j,4)*psi_mk(k,4)/sum_ss(j,4) - thetam_ss(j,5)*psi_mk(k,5)/sum_ss(j,5) - thetam_ss(j,6)*psi_mk(k,6)/sum_ss(j,6) - thetam_ss(j,7)*psi_mk(k,7)/sum_ss(j,7));
end
end
Variable psi_mk is:
1.0000 1.0000 1.2327 1.0339 0.6253 0.7786 0.4064
1.0000 1.0000 1.2327 1.0339 0.6253 0.7786 0.4064
0.7948 0.7948 1.0000 0.6058 0.9254 0.8210 0.3219
0.8324 0.8324 1.5537 1.0000 0.7642 0.8552 0.3371
0.0518 0.0518 0.0897 0.1482 1.0000 0.4899 1.9891
0.4701 0.4701 0.5275 0.9080 0.9192 1.0000 0.1974
0.0191 0.0191 0.0000 0.0663 0.3461 2.5718 1.0000
Variable thetam_ss is:
0.3858 0.6142 0 0 0 0 0
0 0 0.5475 0.4525 0 0 0
0 0.3629 0 0 0.3226 0.3145 0
0 0 0 0 0 0 1.0000
6 个评论
Voss
2022-3-15
I don't know what line the error happens on. sum_ss is indexed on the second line inside the loops. In fact the error could be about trying to access sum_ss(j,2) when k = 1 and sum_ss has only one column, i.e., sum_ss is not pre-allocated to have 7 columns.
q is not indexed with 2 subscripts.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!