For loop with multiple variables

조회 수: 42 (최근 30일)
Vaishali Nagpure
Vaishali Nagpure 2018년 1월 13일
편집: Jan 2018년 1월 13일
As in java we have "for (int i = 0, j = 1, k = 2; i < 5; i++)"
I would like to do as
for st = 1:numel(pointst) , ed = 1:numel(pointed)
What is the way i can do this kind of operation
  댓글 수: 3
Greg
Greg 2018년 1월 13일
How are j and k not acting as constants here?
for (int i = 0, j = 1, k = 2; i < 5; i++)
Vaishali Nagpure
Vaishali Nagpure 2018년 1월 13일
Yes here those are constants what i am trying is for (int i = 1, j = 0; i <= limit; i++, j++)

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 1월 13일
편집: Jan 2018년 1월 13일
If you want this:
Java: for (int i = 1, j = 0; i <= limit; i++, j++)
in Matlab:
j = 0;
for i = 1:limit
j = j + 1;
...
end
Less nice alternatives:
index = [1:limit; ...
1:limit];
for k = 1:size(index, 2)
i = index(1, k);
j = index(2, k);
...
end
or
index = [1:limit; ...
1:limit];
for k = index
i = k(1);
j = k(2);
...
end
The last one might look strange. The FOR loop evaluates the given index vector along the 2nd dimension, such that the column vectors are assigned to k.
Maybe this is useful for you:
indexI = 1:limit;
indexJ = 1:limit; % Of course this is a test data only
for k = 1:numel(index1)
i = indexI(k);
j = indexJ(k);
...
end

추가 답변 (1개)

Rik
Rik 2018년 1월 13일
If you mean a nested loop:
for st = 1:numel(pointst)
for ed = 1:numel(pointed)
end
end
  댓글 수: 1
Vaishali Nagpure
Vaishali Nagpure 2018년 1월 13일
I want pointst first field and pointed first field at the same time in one iteration. Then in second iteration i want second field of pointst and pointed and so on ....

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by