[R] the results of the SORT function differ from Scilab/Matlab for Complex Numbers

Duncan Murdoch murdoch.duncan at gmail.com
Thu Nov 8 22:50:52 CET 2012


On 08/11/2012 4:42 PM, Cleber N.Borges wrote:
> Ok, thanks.
>
> There are a simple mode for emulate this behaviour?

This should sort by modulus then argument (phase):

x[order(Mod(x), Arg(x))]

It does strange things if x happens to be real:

 > x <- (-5):5
 > sort(x)
  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5
 > x[order(Mod(x), Arg(x))]
  [1]  0  1 -1  2 -2  3 -3  4 -4  5 -5

but that may be what you want.

Duncan Murdoch

>
> Cleber
>
> Em 08/11/2012 19:25, Thomas Lumley escreveu:
> > On Fri, Nov 9, 2012 at 10:02 AM, Cleber N.Borges <klebyn at yahoo.com.br
> > <mailto:klebyn at yahoo.com.br>> wrote:
> >
> >     Hello useRs,
> >
> >     The results of the SORT function differ from Scilab/Matlab for
> >     Complex Numbers in my example.
> >     This design is the desirable in R?
> >
> >
> > Well, it's deliberate and documented.
> >
> > R sorts complex numbers by real part then by imaginary part.  Matlab,
> > according to its documentation, sorts by modulus then phase.
> >
> > There isn't a unique way to sort complex numbers, so you're going to
> > get differences.  Personally, I think the R method is more
> > straightforward, since you don't need to decide and remember where the
> > branch cut goes on the phase coordinate.
> >
> >    -thomas
> >
> > --
> > Thomas Lumley
> > Professor of Biostatistics
> > University of Auckland
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list