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

2 Ansichten (letzte 30 Tage)
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,

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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.
  2 Kommentare
Walter Roberson
Walter Roberson am 13 Jul. 2019
thetaDegrees = [90,30,-30];
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];
T_n{K} = subs(T,{l1,m1,n1,l2,m2,n2,l3,m3,n3},{cos(theta_rad),sin(theta_rad),aij_z(1,3),...
-sin(theta_rad),cos(theta_rad),aij_z(2,3),...
aij_z(3,1),aij_z(3,2),aij_z(3,3)});
end
T_n{2}
T_n{3}
Ali Tawfik
Ali Tawfik am 13 Jul. 2019
Hey Walter,
Thanks for your answer.
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,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Geoff Hayes
Geoff Hayes am 12 Jul. 2019
Bearbeitet: Geoff Hayes am 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
thetaDegrees = [90,30,-30];
thetaRadians = thetaDegrees * pi / 180;
  7 Kommentare
Ali Tawfik
Ali Tawfik am 12 Jul. 2019
Bearbeitet: Geoff Hayes am 12 Jul. 2019
Thanks for your answer,
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];
T_n=subs(T,{l1,m1,n1,l2,m2,n2,l3,m3,n3},{cos(theta_rad),sin(theta_rad),aij_z(1,3),...
-sin(theta_rad),cos(theta_rad),aij_z(2,3),...
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 am 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.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 13 Jul. 2019

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by