how to exit from two nested for loop in matlab

大兔子大兔子 提交于 2020-01-23 06:45:30

问题


I have a while loop in which I have two for loops. I have a condition in the innermost for loop. Whenever that condition is satisfied I want to exit from both the two for loops and continue within the while loop:

while (1)
    for x=1:20
        for y=1:30
            if(condition)

            end
        end
    end
end

Does Matlab have something like a labeled statement in Java, or is there another way to do this?


回答1:


Here is a very simple answer leveraging the fact that testing numerous simple conditions is nearly free:

while (1)
    go = true;
    for x=1:20
        for y=1:30
            if go && condition
               go = false;

            end
        end
    end
end

This approach is very simple, easily generalized to any number of loops and avoids the abuse of error handling.




回答2:


One ever so slightly more elegant way than Luis Mendo's. ;-)

while (1)
    for x=1:20
        for y=1:30
            quit = (condition);
            if quit
                break;
            end
        end
        if quit
            break;
        end
    end
end



回答3:


Only slightly more elegant than A.Donda's answer (avoids testing the condition twice):

while 1
    for x=1:20
        for y=1:30
            quit = 0;
            if (condition)
                quit = 1;
                break;
            end
        end
        if quit
            break;
        end
    end
end



回答4:


A bit of abuse, a bit of overkill, but here it is: yet another way

while (1)
    try
        for x = 1 : 20
            for y = 1 : 30
                assert(~(condition), 'break')
            end
        end
    catch err
        if ~strcmp(err.message, 'break'), rethrow(err), end
    end
end

The nice thing about this approach is that it works with an arbitrary number of nested loops, and only evaluates condition once without having to store the result in a variable.

Inspired by tmpearce's comment.




回答5:


I'm aware of only one quite inelegant way to do this: check the condition twice.

while (1)
    for x=1:20
        for y=1:30
            if (condition)
                break;
            end
        end
        if (condition)
            break;
        end
    end
end



回答6:


Another possibility, which avoids using two break statements: if you don't do anything in the outer for loop except calling the inner for loop, you can merge them:

[yy xx] = ndgrid(1:20,1:30);
while 1
    for n = 1:numel(x)
        x = xx(n);
        y = yy(n);
        if (condition)
            break      
        end
    end
end



回答7:


immediate exit for both for loops and continue with the remain of while loop.

while (1)
    go=1 %

    for x=1:20
        if go==0;break; %

        for y=1:30
            if(condition)
                go=0;break; %
            end
        end
    end
end


来源:https://stackoverflow.com/questions/20302746/how-to-exit-from-two-nested-for-loop-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!