Using break/continue in a for loop
2 次查看(过去 30 天)
显示 更早的评论
I have this loop and I want the second loop to have steps with different sizes and I need the inner loop to end after each iteration and go to the next e:
N = 4;
ODN = [ 4 5 4 4];
NP = 17;
ODD = [10000 10000, 5000 5000];
for e = 1:N
for f = 1:ODN(e,1):NP
L = tp(f:ODN(e,1),1);
[X,Y] = min(L,[],1);
xpC(Y,1) = ODD(f,1);
continue
end
end
if I use continue it will continue the inner loop ( which I don't want and if I use break instead, it will reset f to 1. What can I do to get what I want?
0 个评论
采纳的回答
Image Analyst
2022-4-30
Reverse the order of the e anf f loops:
N = 4;
ODN = [ 4 5 4 4];
NP = 17;
ODD = [10000 10000, 5000 5000];
for f = 1:ODN(e,1):NP
for e = 1:N
L = tp(f:ODN(e,1),1);
[X,Y] = min(L,[],1);
xpC(Y,1) = ODD(f,1);
end
end
Now after it does this
L = tp(f:ODN(e,1),1);
[X,Y] = min(L,[],1);
xpC(Y,1) = ODD(f,1);
once, it will move to the next e in the list.
6 个评论
Image Analyst
2022-4-30
You just need one loop to have them both increment in synchrony:
N = 4;
ODN = [ 4 5 4 4];
NP = 17;
ODD = [10000 10000; 5000 5000];
e = [1 2 3 4]
f = [1 5 10 14]
for k = 1 : length(f)
thisf = f(k);
thise = e(k);
L = tp(thisf:ODN(thise,1),1);
[X,Y] = min(L,[],1);
xpC(Y,1) = ODD(thisf,1);
end
更多回答(0 个)
另请参阅
类别
在 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!