Nested loops related question

조회 수: 5 (최근 30일)
Nuno Palma
Nuno Palma 2016년 12월 15일
편집: Jos (10584) 2016년 12월 16일
Let's say I have a similiar loop to this one: ( This two loops are inside another one, but the issue isn't there )
for j = 1:length(path{i})
fprintf('%d ',path{i}(j))
for k = 1:length(dist_parc{i})
fprintf(' (%d) ', dist_parc{i}(k));
end
end
With this loop ( if j = 1:2 ) , what I'm trying to obtain is something like this -> numberfromJ (numberfromK) numberfromJ. But what I'm getting is -> numberfromJ (numberfromK) numberfromJ (numberfromK). So, after the second j loop I didn't want it to go back to k.
What I'm looking for is a way to loop the main one N times and the nested loop only N-1 times. Any way I can do this?
  댓글 수: 1
KSSV
KSSV 2016년 12월 16일
What I'm looking for is a way to loop the main one N times and the nested loop only N-1 times. Any way I can do this?
Cant be achieved like this?
for j = 1:5
for k = 1:j-1
[j,k]
end
end

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

답변 (1개)

Jos (10584)
Jos (10584) 2016년 12월 16일
편집: Jos (10584) 2016년 12월 16일
for j = 1:N
% commands here are executed N times
if j < N
% commands here are executed N-1 times, for example a nested loop:
% for k = ...
% end
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by