Matlab - 在嵌套的if语句中继续下一个elseif [英] Matlab - Continue to next elseif in nested if statements

查看:465
本文介绍了Matlab - 在嵌套的if语句中继续下一个elseif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆嵌套的'if'语句,我不能让它们以我想要的方式流动。我有一个if语句,如果符合,我运行一个唯一性测试(myuniquetest),如果这表明我的条件给出了一个独特的结果,我想记录它并继续。我已经弄清楚了这一部分但是,如果第一个条件没有给出一个唯一的结果,我想继续使用其余的elseif语句来查看后续的那些是否给出了一个独特的结果。 (我在这段代码中总共有8个elseif语句,但我想这个方法是相同的。)我的代码看起来像这样:

I have a bunch of nested 'if' statements in one another, and I can't get them to flow the way I want them. I have an if statement, and if it is met, I run a uniqueness test (myuniquetest), and if that shows that my condition gives a unique result, I want to log it and go on. I've got that part all figured out However, if the first condition does not give a unique result, I want to continue with the rest of the elseif statements to see if the subsequent ones give a unique result. (I have a total of 8 elseif statements within this code, but I imagine the methodology iwll be the same.) My code looks something like this:

if edgedouble(y0-1, x0) == 1 && (y0-1)>=y1 && (y0-1)<=y2;
testpt = [y0-1, x0];
uni = myuniquetest(testpt, mypoints);

    if uni ==1;
    k = k+1;
    mypoints{1,k} = testpt;
    mypoints = testmypoints(mypoints, edgedouble, x1, x2, y1, y2);
    end

elseif edgedouble(y0-1, x0+1) ==1 && (y0-1)>=y1 && (y0-1)<=y2 && ...
    (x0+1)>=x1 && (x0+1)<=x2;
testpt = [y0-1, x0+1];
uni = myuniquetest(testpt, mypoints);

    if uni ==1;
    k = k+1;
    mypoints{1,k} = testpt;
    mypoints = testmypoints(mypoints, edgedouble, x1, x2, y1, y2);
    end

    etc....
end 

我想要它做的是,如果uni == 0,继续执行elseif(condition2)(等等),但目前它只是停止。我尝试在每个嵌入式'if'语句后添加while语句,因此它们看起来像这样:

What I want it to do is, if uni==0, continue onto the elseif(condition2) (and so on), but currently it just stops. I tried adding in a while statement after each of the embedded 'if' statements so they looked like this:

    if uni ==1;
    (log my values, move on)
    end
while uni==0
continue
end

然而,它崩溃了我的其余代码,随后是Matlab。有没有更简单的方法呢?

However, it crashed the rest of my code, and subsequently Matlab. Is there an easier way to do this?

唯一性函数的代码如下:

The code for the uniqueness function is as follows:

function[uni] = myuniquetest(testpoint, mypoints)
mysize = size(mypoints);
for w = 1:mysize(2);
myt = isequal(testpoint, mypoints{1,w});
if myt == 1;
uni = 0;
break
else
uni = 1;
end
end

当条件满足时,所有这些都有效独特,但它不起作用,只是在满足条件时停止但没有唯一性。

All of this works when the conditions are met and are unique, but it doesn't work and just stops when the condition is met but there is not uniqueness.

谢谢!!

推荐答案

我建议你做的是将所有布尔条件作为单个元素放入逻辑数组中,如以及另一个阵列中所有可能的测试点。之后,迭代覆盖所有条件及其相应的测试点,并使用 testmypoints 脚本检查 uni是否= 1 uni = 0 。我们将基本上继续迭代您的所有条件和相应的测试点直到 uni = 1 ,然后我们可以突破循环。

What I would recommend you do is place all of your Boolean conditions as individual elements into a logical array, as well as all of your possible test points in another array. After, iterate over all of your conditions with their corresponding test points and use the testmypoints script to check to see if uni = 1 or uni = 0. We will basically keep iterating over all of your conditions and corresponding test points until uni = 1, then we can break out of the loop.

因此,创建一个名为条件的逻辑数组这样做:

As such, make a logical array called conditions that does something like this:

conditions = [edgedouble(y0-1, x0) == 1 && (y0-1)>=y1 && (y0-1)<=y2;
              edgedouble(y0-1, x0+1) ==1 && (y0-1)>=y1 && (y0-1)<=y2 && (x0+1)>=x1 && (x0+1)<=x2;
              ...
              ...];

将要检查的每个条件放在逻辑数组。接下来,将每个条件的相应测试点放在另一个数组中。我们称之为测试点

Place each condition that you want to check inside a logical array. Next, place your corresponding test points for each condition inside another array. Let's call this testpoints:

testpoints = [y0-1 x0;
              y0-1 x0-1;
              ...
              ...];

testpoints 将是一个2D数组,其中每一行包含一个测试点,该测试点匹配条件中相应位置的条件。现在,您所要做的就是遍历每个条件和相应的点,直到我们点击 uni = 1 。如果 uni = 0 ,则保持循环并检查其他条件,直到 uni = 1 ,或者如果我们用完了要检查的条件,然后这个循环将结束,你将不会记录任何结果。

testpoints will be a 2D array, where each row consists of a test point that matches a condition in the corresponding position in conditions. Now, all you have to do is iterate through each condition and corresponding point until we hit uni = 1. If uni = 0, then keep looping and check the other conditions until uni = 1, or if we run out of conditions to check, then this loop will end and you won't get any results logged.

没有进一步的ado:

for idx = 1 : numel(conditions)
    if conditions(idx)
        testpt = testpoints(idx,:);
        uni = myuniquetest(testpt, mypoints);

        if uni == 1
            k = k + 1;
            mypoints{1,k} = testpt;
            mypoints = testmypoints(mypoints, edgedouble, x1, x2, y1, y2);
            break;
        end
    end
end

让我们慢慢浏览这段代码首先,我们检查特定条件是否为 true 。如果是,那么让我们获得与该位置对应的相应测试点,然后使用 myuniquetest 进行检查。如果 uni == 1 ,那么我们将运行 uni == 1 时发生的代码(说实话,我不知道不明白你在这里做了什么,但如果你说它有效......那么好吧!)。一旦发生这种情况,我们就会突破循环并记录您的结果。如果 uni == 0 ,那么我们的条件不满意,所以我们应该继续检查其他条件。

Let's walk through this code slowly, first, we check to see whether a particular condition is true. If it is, then let's get the corresponding test point that corresponds to this position, then check with your myuniquetest. Should uni == 1, then we will run the code that happens when uni == 1 (truthfully, I don't understand what you're doing here, but if you say it works.... well ok then!). Once this happens, we break out of the loop and your results are logged. If uni == 0, then our condition isn't satisfied, so we should proceed and check the other conditions.

希望这会有所帮助!

这篇关于Matlab - 在嵌套的if语句中继续下一个elseif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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