How to negate an outer loop when you have nested loops?
2 次查看(过去 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!