Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
simple question about <>=& |

Subject: simple question about <>=& |

From: Baalzamon

Date: 3 Jul, 2011 18:09:10

Message: 1 of 3

Say I have a matrix:
Test = [ 5, 1, 1.5, 10]

and I want to check that column 3 is between 0.9 and 1.1 (or equal to)


so 0.9 <=Test(1,3) <= 1.1

Why can I not use the expression above into my if statement like so:

if ( 0.9 <=Test(1,3) <= 1.1 )
disp ('True')
else
disp('False')
end;

The result is True in this case...but it should be false as far as I know
It seems that this expression only evaluates one of the conditions...

So I have to do this
if ( (0.9 <= Test(1,3) ) && (Test(1,3) <= 1.1 ) )

My question therefore is why does the first expression not yield the false statement?

Subject: simple question about <>=& |

From: Roger Stafford

Date: 3 Jul, 2011 20:04:09

Message: 2 of 3

"Baalzamon " <baalzamon_moridin@yahoo.com> wrote in message <iuqb85$4gc$1@newscl01ah.mathworks.com>...
> Say I have a matrix:
> Test = [ 5, 1, 1.5, 10]
> and I want to check that column 3 is between 0.9 and 1.1 (or equal to)
> so 0.9 <=Test(1,3) <= 1.1
> Why can I not use the expression above into my if statement like so:
> if ( 0.9 <=Test(1,3) <= 1.1 )
> disp ('True')
> else
> disp('False')
> end;
>
> The result is True in this case...but it should be false as far as I know
> It seems that this expression only evaluates one of the conditions...
> So I have to do this
> if ( (0.9 <= Test(1,3) ) && (Test(1,3) <= 1.1 ) )
> My question therefore is why does the first expression not yield the false statement?
- - - - - - - - -
  When you write "a<=b<=c" the result is not what you would expect. The proper syntax is (a<=b) & (b<=c).

  You got no error message because matlab interprets your expression this way:

 (a<=b)<=c

The first part (a<=b) was true so its numerical value is 1. Then you would be testing whether 1<=c which was also true and gives a final true result.

  The lesson: never use a<=b<=c when you mean (a<=b)&{b<=c).

Roger Stafford

Subject: simple question about <>=& |

From: Baalzamon

Date: 3 Jul, 2011 20:18:07

Message: 3 of 3

"Roger Stafford" wrote in message <iuqhvp$k84$1@newscl01ah.mathworks.com>...
> "Baalzamon " <baalzamon_moridin@yahoo.com> wrote in message <iuqb85$4gc$1@newscl01ah.mathworks.com>...
> > Say I have a matrix:
> > Test = [ 5, 1, 1.5, 10]
> > and I want to check that column 3 is between 0.9 and 1.1 (or equal to)
> > so 0.9 <=Test(1,3) <= 1.1
> > Why can I not use the expression above into my if statement like so:
> > if ( 0.9 <=Test(1,3) <= 1.1 )
> > disp ('True')
> > else
> > disp('False')
> > end;
> >
> > The result is True in this case...but it should be false as far as I know
> > It seems that this expression only evaluates one of the conditions...
> > So I have to do this
> > if ( (0.9 <= Test(1,3) ) && (Test(1,3) <= 1.1 ) )
> > My question therefore is why does the first expression not yield the false statement?
> - - - - - - - - -
> When you write "a<=b<=c" the result is not what you would expect. The proper syntax is (a<=b) & (b<=c).
>
> You got no error message because matlab interprets your expression this way:
>
> (a<=b)<=c
>
> The first part (a<=b) was true so its numerical value is 1. Then you would be testing whether 1<=c which was also true and gives a final true result.
>
> The lesson: never use a<=b<=c when you mean (a<=b)&{b<=c).
>
> Roger Stafford

This is what I thought and I used to use ( ( statement1) & (statement2 ) )
then I changed it to a <x <b
Thanks for the clarification.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us