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:
IF (simple coding problem)

Subject: IF (simple coding problem)

From: Chris Mc

Date: 11 Feb, 2013 14:34:09

Message: 1 of 5

Hi,

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:

T=301
if 290<T<300
a=1
else
a=2
end

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?

Subject: IF (simple coding problem)

From: dpb

Date: 11 Feb, 2013 15:04:36

Message: 2 of 5

On 2/11/2013 8:34 AM, Chris Mc wrote:
> Hi,
>
> 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:
>
> T=301
> if 290<T<300
...

> It always give me a=1?? To me, this code says if T is between 290 and
> 300...

But you're not Matlab...to Matlab the two relational operators in a row
are translated from left to right and evaluated sequentially since they
both have the same precedence -- so,

290<T --> 1 ("true") and then 1<300 --> also true. IOW, it's the same
as if you had written

if (290<T) < 300

It's bad syntax--instead, write

if T>290 && T<300

...

--

Subject: IF (simple coding problem)

From: someone

Date: 11 Feb, 2013 15:13:23

Message: 3 of 5

"Chris Mc" wrote in message <kfavh1$dfq$1@newscl01ah.mathworks.com>...
> Hi,
>
> 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:
>
> T=301
> if 290<T<300
> a=1
> else
> a=2
> end
>
> 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?

The statement:

if 290<T<300

is valid syntax, but does not do what you think it should.

MATLAB interprets the statement as:

if (290<T)<300

Since 290 is less than T the expression in parentehis evaluates to TRUE (=1).
Now, since 1 is leass than 300, the result of the if statement is TRUE.

I believe what you want is something like:

if (290<T) & (T<300)

Subject: IF (simple coding problem)

From: Chris Mc

Date: 11 Feb, 2013 15:40:10

Message: 4 of 5

Got it. Thanks for your help guys!!

Subject: IF (simple coding problem)

From: Loren Shure

Date: 11 Feb, 2013 19:31:13

Message: 5 of 5


"Chris Mc" <remove.this.c.mccorquodale@axon-cable.co.uk> wrote in message
news:kfavh1$dfq$1@newscl01ah.mathworks.com...
> Hi,
>
> 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:
>
> T=301
> if 290<T<300
> a=1
> else
> a=2
> end
>
> 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?

See this blog post:
http://blogs.mathworks.com/loren/2012/05/14/why-is-answer-to-3-a-7-unexpected/


--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/

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