'For' loop for specified integers (more than one)
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, I know that we can use for loop for specified integers like : for v=[1 5 23 99]. But I want to have more than one variable. I have to calculate every iteration in the loop with given initial condition (x,y,a). For example for:[ (x=1,y=0,a=0.2),(x=2,y=1,a=2) ] etc. Can I write it somehow ?
댓글 수: 1
답변 (2개)
KSSV
2016년 5월 10일
P = [1 0 0.2 ; 2 1 2 ] ;
for i = 1:size(P,1)
x = P(i,1) ; y = P(i,2) ; a = P(i,3) ;
end
댓글 수: 1
Stephen23
2016년 5월 10일
편집: Stephen23
2016년 5월 10일
Note that you should not use i for variable names, as this is the name of the inbuilt imaginary unit:
Ditto for j, size, length, etc, etc.
Stephen23
2016년 5월 10일
편집: Stephen23
2016년 5월 10일
The simplest solution is to use indexing:
X = [1,2,3]
Y = [0,1,2];
A = [0.2,2,20];
for k = 1:numel(A)
x = X(k)
y = Y(k)
a = A(k)
end
댓글 수: 5
Stephen23
2016년 5월 10일
@Eirini Gk: Yes, it is just basic indexing. You just have to keep in mind that MATLAB indexing (like is common in mathematics) starts from one, not zero.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!