Index in position 2 exceeds array bounds (must not exceed 1)

조회 수: 2 (최근 30일)
Dario Miric
Dario Miric 2022년 3월 15일
편집: Dario Miric 2022년 3월 15일
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
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.
Dario Miric
Dario Miric 2022년 3월 15일
편집: Dario Miric 2022년 3월 15일
@Deepak Gupta You are correct. What I need is to calculate sum_ss and than lngamma_mss in two distinct for loops. You are also correct about nk, ng and q.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by