Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: simple question about <>=& |
Date: Sun, 3 Jul 2011 20:18:07 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 34
Message-ID: <iuqipv$m4k$1@newscl01ah.mathworks.com>
References: <iuqb85$4gc$1@newscl01ah.mathworks.com> <iuqhvp$k84$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1309724287 22676 172.30.248.48 (3 Jul 2011 20:18:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 3 Jul 2011 20:18:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2192888
Xref: news.mathworks.com comp.soft-sys.matlab:734735

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