http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694
MATLAB Central Newsreader  IF (simple coding problem)
Feed for thread: IF (simple coding problem)
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 11 Feb 2013 14:34:09 +0000
IF (simple coding problem)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694#897660
Chris Mc
Hi,<br>
<br>
I am having troulbe using the IF operator. Logically, what I am doing makes sense but it doesn't give the correct output so obviously somethings going wrong. Trying to keep it simple (so as not to confuse things), when I input:<br>
<br>
T=301<br>
if 290<T<300<br>
a=1<br>
else<br>
a=2<br>
end<br>
<br>
It always give me a=1?? To me, this code says if T is between 290 and 300 (which it isn't) a=1, any other value and a=2. Why then is it giving me a=1 and how can I rectify this? Is it something to do with the '<' symbols?

Mon, 11 Feb 2013 15:04:36 +0000
Re: IF (simple coding problem)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694#897661
dpb
On 2/11/2013 8:34 AM, Chris Mc wrote:<br>
> Hi,<br>
><br>
> I am having troulbe using the IF operator. Logically, what I am doing<br>
> makes sense but it doesn't give the correct output so obviously<br>
> somethings going wrong. Trying to keep it simple (so as not to confuse<br>
> things), when I input:<br>
><br>
> T=301<br>
> if 290<T<300<br>
...<br>
<br>
> It always give me a=1?? To me, this code says if T is between 290 and<br>
> 300...<br>
<br>
But you're not Matlab...to Matlab the two relational operators in a row <br>
are translated from left to right and evaluated sequentially since they <br>
both have the same precedence  so,<br>
<br>
290<T > 1 ("true") and then 1<300 > also true. IOW, it's the same <br>
as if you had written<br>
<br>
if (290<T) < 300<br>
<br>
It's bad syntaxinstead, write<br>
<br>
if T>290 && T<300<br>
<br>
...<br>
<br>


Mon, 11 Feb 2013 15:13:23 +0000
Re: IF (simple coding problem)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694#897662
someone
"Chris Mc" wrote in message <kfavh1$dfq$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> <br>
> I am having troulbe using the IF operator. Logically, what I am doing makes sense but it doesn't give the correct output so obviously somethings going wrong. Trying to keep it simple (so as not to confuse things), when I input:<br>
> <br>
> T=301<br>
> if 290<T<300<br>
> a=1<br>
> else<br>
> a=2<br>
> end<br>
> <br>
> It always give me a=1?? To me, this code says if T is between 290 and 300 (which it isn't) a=1, any other value and a=2. Why then is it giving me a=1 and how can I rectify this? Is it something to do with the '<' symbols?<br>
<br>
The statement:<br>
<br>
if 290<T<300<br>
<br>
is valid syntax, but does not do what you think it should.<br>
<br>
MATLAB interprets the statement as:<br>
<br>
if (290<T)<300<br>
<br>
Since 290 is less than T the expression in parentehis evaluates to TRUE (=1).<br>
Now, since 1 is leass than 300, the result of the if statement is TRUE.<br>
<br>
I believe what you want is something like:<br>
<br>
if (290<T) & (T<300)

Mon, 11 Feb 2013 15:40:10 +0000
Re: IF (simple coding problem)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694#897667
Chris Mc
Got it. Thanks for your help guys!!

Mon, 11 Feb 2013 19:31:13 +0000
Re: IF (simple coding problem)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326694#897693
Loren Shure
<br>
"Chris Mc" <remove.this.c.mccorquodale@axoncable.co.uk> wrote in message <br>
news:kfavh1$dfq$1@newscl01ah.mathworks.com...<br>
> Hi,<br>
><br>
> I am having troulbe using the IF operator. Logically, what I am doing <br>
> makes sense but it doesn't give the correct output so obviously somethings <br>
> going wrong. Trying to keep it simple (so as not to confuse things), when <br>
> I input:<br>
><br>
> T=301<br>
> if 290<T<300<br>
> a=1<br>
> else<br>
> a=2<br>
> end<br>
><br>
> It always give me a=1?? To me, this code says if T is between 290 and 300 <br>
> (which it isn't) a=1, any other value and a=2. Why then is it giving me <br>
> a=1 and how can I rectify this? Is it something to do with the '<' <br>
> symbols?<br>
<br>
See this blog post: <br>
<a href="http://blogs.mathworks.com/loren/2012/05/14/whyisanswerto3a7unexpected/">http://blogs.mathworks.com/loren/2012/05/14/whyisanswerto3a7unexpected/</a><br>
<br>
<br>
 <br>
Loren<br>
<a href="http://blogs.mathworks.com/loren/">http://blogs.mathworks.com/loren/</a><br>
<a href="http://www.mathworks.com/matlabcentral/">http://www.mathworks.com/matlabcentral/</a>