For loop, Subscript indices must either be real positive integers or logicals ?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ali Tawfik
am 12 Jul. 2019
Kommentiert: Ali Tawfik
am 13 Jul. 2019
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,
0 Kommentare
Akzeptierte Antwort
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
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}
Weitere Antworten (2)
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
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!