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:
3 condition in IF statement

Subject: 3 condition in IF statement

From: Babak

Date: 30 Jul, 2013 19:40:14

Message: 1 of 17

Hi everybody. I have program which 3 condition should be compare. I want this IF statement to be true only in the case where the first condition AND the second condition AND the third condition are all true simultaneously.
Thanks a lot

clc
clear all
f1=3;f2=3;THD=2;
if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
    disp('Normal)')
elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
         disp('Sag')
end

In this program it will always shows "Normal" because of the last condition (1>=THD<=6)!
Where I went wrong?

Subject: 3 condition in IF statement

From: Roger Stafford

Date: 30 Jul, 2013 19:59:21

Message: 2 of 17

"Babak " <b_mn@hotmail.com> wrote in message <kt94qu$h19$1@newscl01ah.mathworks.com>...
> if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> disp('Normal)')
> elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
> disp('Sag')
- - - - - - - - - - - -
Each of your six condition expressions is invalid in matlab syntax. In fact, as they stand, they don't mean what I believe you intend. For example

 "0>=f1<=2"

is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab. You must say "0<=f1 & f1<=2". The same applies to the other five conditions in your code.

Roger Stafford

Subject: 3 condition in IF statement

From: Babak

Date: 30 Jul, 2013 20:12:11

Message: 3 of 17

"Roger Stafford" wrote in message <kt95up$k6l$1@newscl01ah.mathworks.com>...
> "Babak " <b_mn@hotmail.com> wrote in message <kt94qu$h19$1@newscl01ah.mathworks.com>...
> > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> > disp('Normal)')
> > elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
> > disp('Sag')
> - - - - - - - - - - - -
> Each of your six condition expressions is invalid in matlab syntax. In fact, as they stand, they don't mean what I believe you intend. For example
>
> "0>=f1<=2"
>
> is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab. You must say "0<=f1 & f1<=2". The same applies to the other five conditions in your code.
>
> Roger Stafford


Thank you Roger. You were right

Subject: 3 condition in IF statement

From: someone

Date: 30 Jul, 2013 20:59:22

Message: 4 of 17

"Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <kt95up$k6l$1@newscl01ah.mathworks.com>...
> > "Babak " <b_mn@hotmail.com> wrote in message <kt94qu$h19$1@newscl01ah.mathworks.com>...
> > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> > > disp('Normal)')
> > > elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
> > > disp('Sag')
> > - - - - - - - - - - - -
> > Each of your six condition expressions is invalid in matlab syntax. In fact, as they stand, they don't mean what I believe you intend. For example
> >
> > "0>=f1<=2"
> >
> > is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab.

Just to clarify - unfortunately the latter notation IS allowed. But, as you point-out, it doesn't do what the OP thinks it should.

This might be a good addition for the MATLAB FAQ.

> > You must say "0<=f1 & f1<=2".

To get the intended result.

> > The same applies to the other five conditions in your code.
> >
> > Roger Stafford
>
>
> Thank you Roger. You were right

Subject: 3 condition in IF statement

From: Roger Stafford

Date: 30 Jul, 2013 22:10:21

Message: 5 of 17

"Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
- - - - - - -
  'Someone' is right. Strictly speaking, that notation is allowed, but it is interpreted in the following remarkable way. In "4>=f2<=5" suppose f2 = 6. Then the expression is parsed as:

 (4>=f2)<=5

The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true. However this is certainly not what one would expect in such a case.

  Many people have been tripped up by this kind of unintended result. The obvious remedy is to never, never use expressions of a form similar to a<=b<=c in matlab.

Roger Stafford

Subject: 3 condition in IF statement

From: Babak

Date: 30 Jul, 2013 22:24:08

Message: 6 of 17

"Roger Stafford" wrote in message <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
> "Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> - - - - - - -
> 'Someone' is right. Strictly speaking, that notation is allowed, but it is interpreted in the following remarkable way. In "4>=f2<=5" suppose f2 = 6. Then the expression is parsed as:
>
> (4>=f2)<=5
>
> The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true. However this is certainly not what one would expect in such a case.
>
> Many people have been tripped up by this kind of unintended result. The obvious remedy is to never, never use expressions of a form similar to a<=b<=c in matlab.
>
> Roger Stafford

Very interesting! Thank you for more info,nowhere I could find such info

Subject: 3 condition in IF statement

From: dpb

Date: 30 Jul, 2013 23:46:30

Message: 7 of 17

On 7/30/2013 5:24 PM, Babak wrote:
...

>> Many people have been tripped up by this kind of unintended result.
>> The obvious remedy is to never, never use expressions of a form
>> similar to a<=b<=c in matlab.
>>
...

> Very interesting! Thank you for more info,nowhere I could find such info

Look up "operator precedence" in the documentation...

--

Subject: 3 condition in IF statement

From: dpb

Date: 30 Jul, 2013 23:50:15

Message: 8 of 17

On 7/30/2013 5:10 PM, Roger Stafford wrote:
> "Babak " <b_mn@hotmail.com> wrote in message
> <kt96mq$m84$1@newscl01ah.mathworks.com>...
>> > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> - - - - - - -
> 'Someone' is right. Strictly speaking, that notation is allowed, but it
> is interpreted in the following remarkable way. In "4>=f2<=5" suppose f2
> = 6. Then the expression is parsed as:
>
> (4>=f2)<=5
...

I don't see it as "remarkable"; it's simply the result of applying
operator precedence rules. It's not _that_ much different in other
languages other than one might get at least a warning in a more strongly
typed language and an outright error in more stringent ones yet.

That it might not mean what OP thought simply means mostly that OP
hadn't yet read the manual... :)

--

Subject: 3 condition in IF statement

From: Roger Stafford

Date: 31 Jul, 2013 01:48:16

Message: 9 of 17

dpb <none@non.net> wrote in message <kt9jfp$cre$1@speranza.aioe.org>...
> I don't see it as "remarkable"; it's simply the result of applying
> operator precedence rules. It's not _that_ much different in other
> languages other than one might get at least a warning in a more strongly
> typed language and an outright error in more stringent ones yet.
>
> That it might not mean what OP thought simply means mostly that OP
> hadn't yet read the manual... :)
- - - - - - - - - -
  I said "remarkable" in an endeavor to see things from the point of view of someone familiar with mathematical notation but not with that of matlab. The notation a<=b<=c occurs very frequently in mathematics and is therefore an easy mistake to make for someone in the process of learning matlab. In my opinion using opposing inequalities as in "4>=f2<=5" is a more fundamental error.

Roger Stafford

Subject: 3 condition in IF statement

From: Nasser M. Abbasi

Date: 31 Jul, 2013 02:04:25

Message: 10 of 17

On 7/30/2013 5:10 PM, Roger Stafford wrote:

> (4>=f2)<=5
>
> The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values
>at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true.
>However this is certainly not what one would expect in such a case.
>

fyi:

Matlab:
---------------------
EDU>> f=6;
EDU>> 4>= f <= 5
ans =
      1
---------------

Mathematica:
------------------------
f = 6;
4 >= f <= 5

Out[2]= False
----------------------


but Maple rejects it
-------------------------
4>=f<=5;
Error, `<=` unexpected

evalb(4>=f)<=5;
                  false <= 5
--------------------------

Maple uses true/false for result of (4>=f), but that gives
a type that can't be used to be compared with a number.

hence final result is left unevaluated.

--Nasser

Subject: 3 condition in IF statement

From: dpb

Date: 31 Jul, 2013 03:55:08

Message: 11 of 17

On 7/30/2013 9:04 PM, Nasser M. Abbasi wrote:
> On 7/30/2013 5:10 PM, Roger Stafford wrote:
>
>> (4>=f2)<=5
>>
>> The "4>=f2" portion would be false which gives "false<=5", but since
>> the "<=" demands numerical values
>> at either end, the 'false' is translated to a numerical 0, which gives
>> "0<=5" and that is actually true.
>> However this is certainly not what one would expect in such a case.
>>
>
> fyi:
>
> Matlab:
> ---------------------
> EDU>> f=6;
> EDU>> 4>= f <= 5
> ans =
> 1
> ---------------
>
> Mathematica:
> ------------------------
> f = 6;
> 4 >= f <= 5
>
> Out[2]= False
> ----------------------
>
>
> but Maple rejects it
> -------------------------
> 4>=f<=5;
> Error, `<=` unexpected
>
> evalb(4>=f)<=5;
> false <= 5
> --------------------------
>
> Maple uses true/false for result of (4>=f), but that gives
> a type that can't be used to be compared with a number.
> hence final result is left unevaluated.

Fortran

program precedence
   integer :: f=6

   write(*,*) 4 >= f <= 5
end program

C:\Temp> df /nologo preced.f90
preced.f90

C:\Temp> preced
  T

--

Subject: 3 condition in IF statement

From: dpb

Date: 31 Jul, 2013 04:13:54

Message: 12 of 17

On 7/30/2013 9:04 PM, Nasser M. Abbasi wrote:
> On 7/30/2013 5:10 PM, Roger Stafford wrote:
>
>> (4>=f2)<=5
>>
>> The "4>=f2" portion would be false which gives "false<=5", but since
>> the "<=" demands numerical values
>> at either end, the 'false' is translated to a numerical 0, which gives
>> "0<=5" and that is actually true.
>> However this is certainly not what one would expect in such a case.
>>
...

Fortran

program precedence
   integer :: f=6

   write(*,*) 4 >= f <= 5
end program

C:\Temp> df /nologo preced.f90
preced.f90

C:\Temp> preced
  T

NB: Above uses a vendor extension that allows the first logical result
of .F. to then be compared to an integer which as in Matlab converts the
internal value of -1 to integer and that is also <=5 so result is .T.

If one verifies Standard conformance, then the silent conversion is
non-conforming and one will get an error w/ a conforming compiler.

--

Subject: 3 condition in IF statement

From: Marc

Date: 31 Jul, 2013 05:04:11

Message: 13 of 17

dpb <none@non.net> wrote in message <kta2u3$de5$1@speranza.aioe.org>...
> On 7/30/2013 9:04 PM, Nasser M. Abbasi wrote:
> > On 7/30/2013 5:10 PM, Roger Stafford wrote:
> >
> >> (4>=f2)<=5
> >>
> >> The "4>=f2" portion would be false which gives "false<=5", but since
> >> the "<=" demands numerical values
> >> at either end, the 'false' is translated to a numerical 0, which gives
> >> "0<=5" and that is actually true.
> >> However this is certainly not what one would expect in such a case.
> >>
> ...
>
> Fortran
>
> program precedence
> integer :: f=6
>
> write(*,*) 4 >= f <= 5
> end program
>
> C:\Temp> df /nologo preced.f90
> preced.f90
>
> C:\Temp> preced
> T
>
> NB: Above uses a vendor extension that allows the first logical result
> of .F. to then be compared to an integer which as in Matlab converts the
> internal value of -1 to integer and that is also <=5 so result is .T.
>
> If one verifies Standard conformance, then the silent conversion is
> non-conforming and one will get an error w/ a conforming compiler.
>
> --

We are having a Matlab Hall of Fame discussion... Which I am definitely not worthy.

As for Maple 17, I got "false". No error but I always wonder if I am using the correct formats with these later versions.


f := 6;
print(`output redirected...`); # input placeholder
                               6
4 >= f;
print(`output redirected...`); # input placeholder
                             6 <= 4
4 >= f and f <= 5;
print(`output redirected...`); # input placeholder
                             false
Notice on my copy and paste HOW maple 17 handles this.....

4>=f<=5 .... is what I typed in and when I copied and pasted it, all of sudden it seems to change things as I would have intended?

Subject: 3 condition in IF statement

From: dpb

Date: 31 Jul, 2013 15:20:34

Message: 14 of 17

On 7/30/2013 8:48 PM, Roger Stafford wrote:
> dpb <none@non.net> wrote in message <kt9jfp$cre$1@speranza.aioe.org>...
>> I don't see it as "remarkable"; it's simply the result of applying
>> operator precedence rules. It's not _that_ much different in other
>> languages other than one might get at least a warning in a more
>> strongly typed language and an outright error in more stringent ones yet.
>>
>> That it might not mean what OP thought simply means mostly that OP
>> hadn't yet read the manual... :)
> - - - - - - - - - -
> I said "remarkable" in an endeavor to see things from the point of view
> of someone familiar with mathematical notation but not with that of
> matlab. The notation a<=b<=c occurs very frequently in mathematics and
> is therefore an easy mistake to make for someone in the process of
> learning matlab. In my opinion using opposing inequalities as in
> "4>=f2<=5" is a more fundamental error.
>
> Roger Stafford

I voted against "remarkable" owing to the 60-yr precedent of FORTRAN for
precisely the same implementation/interpretation (w/ the noted extension
of casting a LOGICAL to INTEGER which is at least moderately common if
not universal).

I would presume it's that Matlab began as being implemented in FORTRAN
that caused such specific interpretation and that it has remained that
way for consistency's sake as much or more than for any other reason to
change.

Perhaps my background colors my observations too much that I can't
separate out that programming in any language _isn't_ mathematics
despite how much (or little) any given one tries to mimic mathematical
operations. Naming it FORmulaTRANslation, after all, is pretty close to
saying they tried. :)

--

Subject: 3 condition in IF statement

From: Loren Shure

Date: 1 Aug, 2013 12:31:52

Message: 15 of 17


"Babak " <b_mn@hotmail.com> wrote in message
news:kt9ee8$bt7$1@newscl01ah.mathworks.com...
> "Roger Stafford" wrote in message
> <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
>> "Babak " <b_mn@hotmail.com> wrote in message
>> <kt96mq$m84$1@newscl01ah.mathworks.com>...
>> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
>> - - - - - - -
>> 'Someone' is right. Strictly speaking, that notation is allowed, but
>> it is interpreted in the following remarkable way. In "4>=f2<=5" suppose
>> f2 = 6. Then the expression is parsed as:
>>
>> (4>=f2)<=5
>>
>> The "4>=f2" portion would be false which gives "false<=5", but since the
>> "<=" demands numerical values at either end, the 'false' is translated to
>> a numerical 0, which gives "0<=5" and that is actually true. However
>> this is certainly not what one would expect in such a case.
>>
>> Many people have been tripped up by this kind of unintended result.
>> The obvious remedy is to never, never use expressions of a form similar
>> to a<=b<=c in matlab.
>>
>> Roger Stafford
>
> Very interesting! Thank you for more info,nowhere I could find such info

I wrote about this on my blog last year:

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

--
--Loren

http://blogs.mathworks.com/loren

Subject: 3 condition in IF statement

From: someone

Date: 1 Aug, 2013 16:00:13

Message: 16 of 17

"Loren Shure" wrote in message <ktdkfo$nvs$1@newscl01ah.mathworks.com>...
>
> "Babak " <b_mn@hotmail.com> wrote in message
> news:kt9ee8$bt7$1@newscl01ah.mathworks.com...
> > "Roger Stafford" wrote in message
> > <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
> >> "Babak " <b_mn@hotmail.com> wrote in message
> >> <kt96mq$m84$1@newscl01ah.mathworks.com>...
> >> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> >> - - - - - - -
> >> 'Someone' is right. Strictly speaking, that notation is allowed, but
> >> it is interpreted in the following remarkable way. In "4>=f2<=5" suppose
> >> f2 = 6. Then the expression is parsed as:
> >>
> >> (4>=f2)<=5
> >>
> >> The "4>=f2" portion would be false which gives "false<=5", but since the
> >> "<=" demands numerical values at either end, the 'false' is translated to
> >> a numerical 0, which gives "0<=5" and that is actually true. However
> >> this is certainly not what one would expect in such a case.
> >>
> >> Many people have been tripped up by this kind of unintended result.
> >> The obvious remedy is to never, never use expressions of a form similar
> >> to a<=b<=c in matlab.
> >>
> >> Roger Stafford
> >
> > Very interesting! Thank you for more info,nowhere I could find such info
>
> I wrote about this on my blog last year:
>
> http://blogs.mathworks.com/loren/2012/05/14/why-is-answer-to-3-a-7-unexpected/
>
> --
> --Loren
>
> http://blogs.mathworks.com/loren


I think it would be nice to have Loren's discussion (or a paraphrase of it) in the MATLAB FAQ.

Is that possible/allowable/permissable?

Subject: 3 condition in IF statement

From: dpb

Date: 1 Aug, 2013 16:52:54

Message: 17 of 17

On 8/1/2013 11:00 AM, someone wrote:
...

>
> I think it would be nice to have Loren's discussion (or a paraphrase of
> it) in the MATLAB FAQ.
>
> Is that possible/allowable/permissable?

Probably also could stand some amplification in the 'logical
expressions/operators' section...

--

Tags for 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