MATLAB: I want to restart the loop from starting when a specific condition is met.

MATLABrestart loop from initial condition

a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
for i = 1:5
for j = 1:6
s = s + a(i,j);
end
if s > 20
f = f+1;
a = a/f;
break
end
for k = 1:5
s = s - a(k,i);
end
Now here I want to restart the first for loop from i = 1 when s > 20 and operate f = f+1. If I use break statement it terminates the loop and starts from next condition.

Best Answer

Sanyam - perhaps use a while loop instead.
a = table2array(BRTSTestData);
c = 20;
f = 1;
s = 0;
i = 1;
while i <= 5
for j = 1:6
s = s + a(i,j);
end
if s > 20
f = f+1;
a = a/f;
i = 1; % <--- reset i to one
break;
end
for k = 1:5
s = s - a(k,i);
end
i = i + 1; % <--- increment i
end