How to negate an outer loop when you have nested loops?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a series of nested loops in my program. I would like to be able to type something like, "Radfrac_min=1; Radfrac_step=0; Radfrac_max=1; and just essentially ignore that outside loop and continue into the second nested loop. Is there any way to do this without making another if statement at the beginning and then copying the whole program and pasting it after an elseif statement? Is there a command that will do it? ' break' just terminates the program.
Thanks! * * bold**
for radfrac= Radfrac_min : Radfrac_step : Radfrac_max
if Radfrac_min==1
disp('Radfrac_min must not be 1.')
break %Should resolve Radfrac_min!=1 problem and just
%exit the loop.
elseif Radfrac_step==0
disp('Radfrac_step must not be 0')
break
elseif Radfrac_min==Radfrac_max
disp('Radfrac_min may not equal Radfrac_max.')
break
else radfrac_iteration_count=radfrac_iteration_count+1;
end
for u=1:Max_training
for v=0:dimension
for i=1:z
...
end
end
end
end
댓글 수: 2
KSSV
2016년 11월 18일
Note that
Radfrac_step=0;
cannot be zero if you are using it like
radfrac= Radfrac_min : Radfrac_step : Radfrac_max ;
It will lead to empty matrix.
per isakson
2016년 11월 18일
편집: per isakson
2016년 11월 18일
"Is there a command that will do it?"   The simple answer is NO. AFAIK.
BTW: "' break' just terminates the program."   See break, Terminate execution of for or while loop
답변 (1개)
John D'Errico
2016년 11월 18일
편집: John D'Errico
2016년 11월 18일
Where you put the break is in the outer-loop. When it hits the break, OF COURSE it will terminate the main loop! You told it to break out of the loop!
When you break out of a loop, you cannot hope that MATLAB will magically know to execute something that is also inside the loop, but farther down. Computers cannot read your mind. They do exactly as told. When it hits the break, it breaks out. Period.
Sadly, your code is pretty confusing as to what you really want to do. I would suggest that:
You should not be making these tests inside the loop! For example, all of the variables that you are testing are fixed constants,or they should be. Radfrac_min, Radfrac_step, and Radfrac_max are all loop parameters. I assume they do not change inside those loops, but it is a terribly bad way to write code if they do. If there is a problem with them, test and resolve those problems before the main loop ever starts!
I might suggest that you could set this up with the outer loop as a while loop. Take those silly blasted tests outside of the main loop completely. But a while loop can be set to run as long as the test that controls it tells it to run. I think this will work, but again, your code is terribly confusing as to what you truly want to happen, since the code as you have written it won't do what you claim to want to do.
댓글 수: 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!