"Or Statement in And statement" using if operator

Hello everyone,
I have a very simple question....and I have been working on it for some time but cannot figure it out. I try to write the statement that condition 1 : Test(i-1) or Test(i-2) =0, condition 2: Test(i+1) or Test(i+2) =0, when condition 1 and condition 2 are true then the statement is true. I tried to write the code but it did not work at all.
for i = 3:N-2
if Test(i-1)==0||Test(i-2)==0 && Test(i+1)==0||Test(i+2)==0
end
end
Thanks for the help in advance :)

 Accepted Answer

Stephen23
Stephen23 on 30 May 2016
Edited: Stephen23 on 30 May 2016
You need to use brackets to group the logical statements together:
if (A || B) && (C || D)
Otherwise the operator precedence rules gives the order in which they are evaluated.
Note that you should not use i (or j) for loop variable names, as these are names of the inbuilt imaginary unit.

5 Comments

Thanks for your answers. But when I checked the result, it did not do the 'and' statement.
According to my statement, the data 396 and 407 should not refill the data.
@Doris: you showed a screenshot of some numeric values. Sadly my crystal ball is not working today, so I have no idea how you generated these values, nor what you expected them to be. We are happy to help, but you actually need to give us useful information so that we can help you:
  • your exact code
  • any inputs required
  • a description of what you expected to happen
I suspect that the problem is that you are checking if floating-point numbers are equal to zero, after having performed some calculations. Essentially what is displayed as zero is not really numerically equal to zero, and so this equality calculation fails.
The solution is to compare the absolute difference against some tolerance value. You will find lots of discussions on this forum about this topic:
You also might like to try James Tursa's FEX submission for even more insight into floating point number values:
Doris
Doris on 31 May 2016
Edited: Doris on 31 May 2016

Thank you so much for your patience. I haven't really familiar with how to ask a question here.

for k = 3:N-2
if Test(k)==0
    if (Test(k-1)==0||Test(k-2)==0) && (Test(k+1)==0||Test(k+2)==0)
        if count <= 4
            count = 0;
        else
            m= m+1;
            sample1(m,1:count) = Heat_refill(k-count:k-1);
            sample2(m,1:count) = Forward_refill(k-count:k-1); 
            Indice(m,1:count)  = [k-count:k-1];
            count = 0;
        end
    else 
        Heat_refill(k)= Div_Heatload14(k);
        Forward_refill(k)= Div_Forward14(k);
        count = count+1;
    end 
else 
    count = count+1;
end 
end

Here is my exact code.The 'test' data contains non-zero values and zeros, which I want to select and refill certain values based on following flow chart(sorry for cannot insert the chart vertically). But as the result I shown previously, the second if statement didn't do its work.

In my last comment I explained why that test probably failed (because the value isn't really zero). The solution to this is also explained in my last comment.
If you upload a .mat file with those value then I can check the values, and show you how you can compare them in a more robust manner.
Thank you so much. I have solved this problem. The float number was the biggest problem and I also wrote the if statement in wrong way.

Sign in to comment.

More Answers (1)

hi i got same issue when i running script as below :
if ((rate ~= rate_tx || (Nbpsc ~= Nbpsc_tx) || (psdu_byte ~= psdu_byte_tx)))
Percounter = 1;
noviterbi_Y = [];
PSDU = [];
return ;
else
Percounter = 0;
help me please

Asked:

on 30 May 2016

Edited:

on 24 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!