For loop, Subscript indices must either be real positive integers or logicals ?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2019년 7월 12일
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
Walter Roberson
2019년 7월 13일
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}
추가 답변 (2개)
Geoff Hayes
2019년 7월 12일
편집: Geoff Hayes
2019년 7월 12일
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
Geoff Hayes
2019년 7월 12일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!