Thread Subject: i <= 0 ??

Subject: i <= 0 ??

From: Muhammad

Date: 30 Oct, 2009 20:55:18

Message: 1 of 7

Complex number are ordered in matlab. For the imaginary unit, i, for example, I get
? disp(i < 0 | i == 0)
ans =
     0
This may be consistent behavior. But it is certainly inconsistent with the following result:
? disp (i <= 0)
ans =
     1

Subject: i <

From: Nathan

Date: 30 Oct, 2009 21:05:44

Message: 2 of 7

On Oct 30, 1:55 pm, "Muhammad " <muh...@shaw.ca> wrote:
> Complex number are ordered in matlab. For the imaginary unit, i, for example, I get
> ? disp(i < 0 | i == 0)
> ans =
>      0
> This may be consistent behavior. But it is certainly inconsistent with the following result:
> ? disp (i <= 0)
> ans =
>      1

That's interesting:
>> disp(i>0)
     0
>> disp(i>=0)
     1
>> disp(i==0)
     0
>> disp(i<=0)
     1
>> disp(i<0)
     0

But then again, why would you compare i to zero anyways?

-Nathan

Subject: i <= 0 ??

From: Bruno Luong

Date: 30 Oct, 2009 21:15:20

Message: 3 of 7

From the doc:

[... The operators <, >, <=, and >= use only the real part of their operands for the comparison. The operators == and ~= test real and imaginary parts. ..]

And to confuse even more, SORT uses another relation ordering (I believe the *magnitude* is compared)

>> sort([1+1i 2i])

ans =

   1.0000 + 1.0000i 0 + 2.0000i

>> 1+1i<=2i

ans =

     0

% Bruno

Subject: i <= 0 ??

From: Bruno Luong

Date: 30 Oct, 2009 21:22:01

Message: 4 of 7

And you have not finish with another astonishing rule:

>> sort([0 1 -2])

ans =

    -2 0 1

 >> sort([1i 1 -2])

ans =

   1.0000 0 + 1.0000i -2.0000 % 1 <= -2 !!!


% Bruno

Subject: i <= 0 ??

From: Nasser M. Abbasi

Date: 30 Oct, 2009 21:38:30

Message: 5 of 7


"Muhammad " <muhali@shaw.ca> wrote in message
news:hcfjrm$jcs$1@fred.mathworks.com...
> Complex number are ordered in matlab. For the imaginary unit, i, for
> example, I get
> ? disp(i < 0 | i == 0)
> ans =
> 0
> This may be consistent behavior. But it is certainly inconsistent with the
> following result:
> ? disp (i <= 0)
> ans =
> 1

Ordering where complex numbers are involved should be based on the real
parts. If 2 complex numbers have the same real part, then the imaginary part
(its absolute value) should break the tie.

In the first example, i<0 , here since real part of i which is 0 is NOT
less than zero then i<0 is false. For i==0, since the real parts are equal
(both are zero) then we look at the imaginary part to break the tie. Since
the abs value of the imaginary part of i (which is 1) is clearly not equal
to the imaginary part of zero, which is zero, then we get False again.
Hence the result false Or False is False as shown.

For the case of i<=0, the real part of i is zero, and the real part of 0 is
zero, so it is a tie, looking at the imaginary parts, the absolute value of
the imaginary part of i is 1, and the for 0 it is zero, hence 1<=0 should be
False.

Hence Matlab answer for i<=0 should be 0 not 1. This is a bug. Or Matlab
uses some other logic to decide on these issues.

--Nasser

Subject: i <= 0 ??

From: James Tursa

Date: 30 Oct, 2009 22:20:19

Message: 6 of 7

"Nasser M. Abbasi" <nma@12000.org> wrote in message <s1JGm.37806$Tx1.13127@newsfe15.iad>...
>
> Hence Matlab answer for i<=0 should be 0 not 1. This is a bug.

No.

> Or Matlab uses some other logic to decide on these issues.

Yes, they use the rules they list in the doc for these operators. See Bruno's earlier post for the rules in the doc:

[... The operators <, >, <=, and >= use only the real part of their operands for the comparison. The operators == and ~= test real and imaginary parts. ..]

I don't see any discrepancy between this example and their documented rules. Now if you don't like their documented rules, that is another matter ...

James Tursa

Subject: i <= 0 ??

From: Matt

Date: 8 Nov, 2009 16:17:01

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hcfldp$pr1$1@fred.mathworks.com>...
> And you have not finish with another astonishing rule:
>
> >> sort([0 1 -2])
>
> ans =
>
> -2 0 1
>
> >> sort([1i 1 -2])
>
> ans =
>
> 1.0000 0 + 1.0000i -2.0000 % 1 <= -2 !!!

Hazardous indeed. A likely pitfall is, for example, if you want to sort the result of an ifft() output. Often you expect the output to be real-valued, and would expect the sorting to be done according to the real-part only, but forget that the output will have a residual complex part due to numerical round-off.

I guess it's worth cooking up a realsort() function and getting into the habit of using that...

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
obscure behavior Matt J 30 Oct, 2009 17:49:57
ordering Muhammad 30 Oct, 2009 16:59:06
complex numbers Muhammad 30 Oct, 2009 16:59:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com