How to use the break command in nested loops?

Hi, I have a problem using the break command in the following code. When I use the break I exit the loop j, but I want to restart the loop i after the condition. Any idea how can I do in this case? Thanks inm advance.
for i = 1:length(initial_solution.position) - 1
itabu = i;
for j = i + 1:length(initial_solution.position)
jtabu = j;
t = CHECK_MOVE(itabu, jtabu, TL);
if (t == 1) % 0 or 1 / True or False / Tabu or not Tabu
break
end
neighbor.position = SWAP(initial_solution.position, itabu, jtabu);
% Evaluate Objective Function
neighbor = EVALUATE_OF(neighbor);
% Update best neighbor
if neighbor.OF >= best_neighbor.OF
best_neighbor = neighbor;
end
end
end

댓글 수: 1

AndresVar
AndresVar 2022년 2월 17일
i think you need another outter while loop to repat the whole thing. And a flag to tell it to stop when all the i and js were done.

댓글을 달려면 로그인하십시오.

답변 (1개)

David Hill
David Hill 2022년 2월 17일
편집: David Hill 2022년 2월 17일

0 개 추천

while 1
flag=0;
for i = 1:length(initial_solution.position) - 1
itabu = i;
for j = i + 1:length(initial_solution.position)
jtabu = j;
t = CHECK_MOVE(itabu, jtabu, TL);
if (t == 1) % 0 or 1 / True or False / Tabu or not Tabu
flag=1;
break
end
neighbor.position = SWAP(initial_solution.position, itabu, jtabu);
% Evaluate Objective Function
neighbor = EVALUATE_OF(neighbor);
% Update best neighbor
if neighbor.OF >= best_neighbor.OF
best_neighbor = neighbor;
end
end
if flag
break;
end
end
if ~flag
break;
end
end

댓글 수: 1

What? No failsafe to prevent an infinite loop? That's not very robust.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

Yro
2022년 2월 17일

댓글:

2022년 2월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by