0
votes

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 

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

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.

Thanks!!

1
can show how condition1 looks like? example of that would do.lakshmen
I did, thanks! I am having that issue with the 'switch' function now!Marissa

1 Answers

3
votes

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 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.

Without further 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

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.


Hope this helps!