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

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 Commenti

The error could be about indexing thetam_ss or sum_ss.
Dario Miric
Dario Miric il 15 Mar 2022
Modificato: Dario Miric il 15 Mar 2022
Well, error says that index on position 2 must not exceed value of 1. Variable thetam_ss is (4x7) matrix and error says that value of 1 can't be exceeded on position 2 which suggests that somehow has only 1 column. Variable sum_ss shouldn't be a problem because program doesn't access it's values unlike thetam_ss. Variable is defined as matrix of (nk x ng) dimensions and code should write calculated values in every element of the matrix.
Your error is coming because of wrong indexing for sum_ss. In the beginning when j = 1 and k=1, you are using sum_ss(j, 1), sum_ss(j, 2).... sum_ss(j,7) in the calculation of lngammma_mss. Except sum_ss(1, 1) no other value exists. I am assuming you have not predefined sum_ss with some value and this is what's causing the error.
What about q(k)? What's it's value? And what are nk and ng?
sum_ss = ones(nk, ng);
for j = 1:nk
for k = 1:ng
sum_ss(j,k) = sum(thetam_ss(j,:)'.*psi_mk(:,k));
lngammma_mss(j,k) = q(k)*(1 - log(sum_ss(j,k)) - sum(thetam_ss(j,:).*psi_mk(k,:)./sum_ss(j,:)));
end
end
I am not sure (as there are so many unknowns) but you can try if this code does what you want. Here i am making assumptions that nk = 4, ng = 7 and q is a 1-by-7 vector.
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 il 15 Mar 2022
Modificato: Dario Miric il 15 Mar 2022
@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.

Accedi per commentare.

Risposte (0)

Prodotti

Release

R2020a

Richiesto:

il 15 Mar 2022

Modificato:

il 15 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by