How to Keep Nested For Loop Indexes From Equaling Each Other
2 次查看(过去 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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!