For loop, Subscript indices must either be real positive integers or logicals ?

Asked by Ali Tawfik

Ali Tawfik (view profile)

on 12 Jul 2019
Latest activity Commented on by Ali Tawfik

Ali Tawfik (view profile)

on 13 Jul 2019
Accepted Answer by Walter Roberson

Walter Roberson (view profile)

Hi,
I have been trying the following commands, but I could not, and got that error, (I got that error, Subscript indices must either be real positive integers or logicals.),
for i=[90,30,-30]
theta(i)=i*pi/180
end
& also, when type length of (i) it equals =-30, how ?, and I have 3 variables ?
actually, I hope to have 3 variables (one at i =90, and another =30, and so on ]
So please could you tell me how could I do this ?
Thanks,

Tags

Answer by Walter Roberson

Walter Roberson (view profile)

on 12 Jul 2019

https://www.mathworks.com/matlabcentral/answers/470959-loop-with-two-variables#answer_382585 shows a general structure you can use for looping with values that are not small consecutive integers.

Walter Roberson

Walter Roberson (view profile)

on 13 Jul 2019
for K = 1 : 3
thetaRadians = thetaDegrees(K) * sym(pi) / 180;
T=[l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
aij_z(3,1),aij_z(3,2),aij_z(3,3)});
end
T_n{2}
T_n{3}
Ali Tawfik

Ali Tawfik (view profile)

on 13 Jul 2019
Hey Walter,
It works finally :), that excatly, what I want!
but just I need to obtain another T_n normally without subsitutions, because it did not work with subs ,
just one more thing, how could I also, extract values for thetaradians ? like T_n {}, because when I assign thetaRadians{} , it did not work with the rest of commands,
hope you understand me, and help me .
Again,
I just need to extract specfic values also for thetaRadians (e.g. thetaradians{2@30})
Also, how can I apply loop with subs ?
Thanks,

Answer by Geoff Hayes

Geoff Hayes (view profile)

on 12 Jul 2019
Edited by Geoff Hayes

Geoff Hayes (view profile)

on 12 Jul 2019

Ali - your for loop is iterating over the array [90, 30, -30] so your i is becoming each of these values. That is fine (for the equation) but fails when you use i as an index into your array and i is -30 (and so the error message makes sense). You can avoid the loop and just do
thetaRadians = thetaDegrees * pi / 180;

Geoff Hayes

Geoff Hayes (view profile)

on 12 Jul 2019
Ali - what other commands should be added so that you need a for loop? Can't you just extend the above as
anglesDegrees = [-90 -60 -30 0 30 60 90];
degreeToRadianMap = containers.Map(anglesDegrees, anglesDegrees * pi / 180);
?
Ali Tawfik

Ali Tawfik (view profile)

on 12 Jul 2019
Actually, I need to add the following commands,
T=[l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
aij_z(3,1),aij_z(3,2),aij_z(3,3)});
So I could have three different (T_n) based on each angle ?
It is not working with your previous command, It works with for loop, but how could again to extract values for a specfic values ?
Thanks,
Geoff Hayes

Geoff Hayes (view profile)

on 12 Jul 2019
I think that you need to show more of your code. Are you using the Symbolic Toolbox? And please describe why you need to extract values for a specfic values.