Using break/continue in a for loop

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?

 Accepted Answer

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 Comments

Sherwin
Sherwin on 30 Apr 2022
Edited: Sherwin on 30 Apr 2022
but it still resets f after changing e. I need f to start from 1, then jump to 5 and then 10, and then 14 based on the ODN vector.
Please list the values of e and f that you expect to see, in order, in your inner loop.
e = [1 2 3 4]
f = [1 5 10 14]
Please list what I asked for : the values of e and f in the inner loop on each interation. Like do you want these in the inner loop in this order:
e=1, f=1
e=1, f=5
e=1, f=10
e=1, f=14
e=2, f=1
e=2, f=5
e=2, f=10
e=2, f=14
e=3, f=1
e=3, f=5
e=3, f=10
e=3, f=14
e=4, f=1
e=4, f=5
e=4, f=10
e=4, f=14
Or do you maybe want
e=1, f=1
e=2, f=5
e=3, f=10
e=4, f=14
Or do you maybe want
e=1, f=1
e=2, f=1
e=3, f=1
e=4, f=1
e=1, f=5
e=2, f=5
e=3, f=5
e=4, f=5
e=1, f=10
e=2, f=10
e=3, f=10
e=4, f=10
e=1, f=14
e=2, f=14
e=3, f=14
e=4, f=14
Sorry but I'm quitting for the day so I'll check tomorrow.
sorry I didn't understand the question before. Yes the second one is what I want:
e=1, f=1
e=2, f=5
e=3, f=10
e=4, f=14
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

Sign in to comment.

More Answers (0)

Categories

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!