如何从matlab中的两个嵌套for循环中退出 [英] how to exit from two nested for loop in matlab

查看:1195
本文介绍了如何从matlab中的两个嵌套for循环中退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,其中有两个for循环.我在最里面的for循环中有一个条件.每当满足该条件时,我都希望从两个for循环中退出并继续在while循环中:

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

Matlab在Java中是否有类似于标记语句的内容,或者还有另一种方法可以做到这一点?

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

推荐答案

以下是利用测试多个简单条件几乎是免费的事实的非常简单的答案:

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.

这篇关于如何从matlab中的两个嵌套for循环中退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆