How to Keep Nested For Loop Indexes From Equaling Each Other
조회 수: 1(최근 30일)
표시 이전 댓글
If I have two nested for loops like this, is there a way to skip the iteration where i=j?
for i=1:50
for j=1:50
code
end
end
댓글 수: 0
채택된 답변
Jason Nicholson
2020년 5월 5일
편집: Jason Nicholson
2020년 5월 5일
Use the continue keyword.
for i=1:50
for j=1:50
if j==i
continue; % breaks the inner for loop at the current iteration
else
% Do stuff here
fprintf('i=%d, j=%d\n',i,j);
end
end
end
If this response answers your question, please click "accept this answer."
댓글 수: 2
추가 답변(2개)
참고 항목
범주
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!