Cleber N.Borges
2012-Nov-08 21:02 UTC
[R] the results of the SORT function differ from Scilab/Matlab for Complex Numbers
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? Thanks. Cleber r <- c( 1.7507+0.1689i, 1.7507-0.1689i, 1.3886+0.0000i, 1.0458+0.0792i, 1.0458-0.0792i, 0.8279+0.1861i, 0.8279-0.1861i, 0.8263+0.3731i, 0.8263-0.3731i, 0.6548+0.0000i ) > cbind(sort(r, d=T)) [,1] [1,] 1.7507+0.1689i [2,] 1.7507-0.1689i [3,] 1.3886+0.0000i [4,] 1.0458+0.0792i [5,] 1.0458-0.0792i [6,] 0.8279+0.1861i [7,] 0.8279-0.1861i [8,] 0.8263+0.3731i [9,] 0.8263-0.3731i [10,] 0.6548+0.0000i > Scilab -->gsort(r) ans 1.7507 + 0.1689i 1.7507 - 0.1689i 1.3886 1.0458 + 0.0792i 1.0458 - 0.0792i 0.8263 + 0.3731i 0.8263 - 0.3731i 0.8279 + 0.1861i 0.8279 - 0.1861i 0.6548 > sessionInfo() R version 2.15.2 (2012-10-26) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 [3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C [5] LC_TIME=Portuguese_Brazil.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] R.matlab_1.6.1 R.oo_1.10.2 R.methodsS3_1.4.2 loaded via a namespace (and not attached): [1] MASS_7.3-22 tools_2.15.2 >
Thomas Lumley
2012-Nov-08 21:25 UTC
[R] the results of the SORT function differ from Scilab/Matlab for Complex Numbers
On Fri, Nov 9, 2012 at 10:02 AM, Cleber N.Borges <klebyn@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]]