2014-04-20 3 views
12

Ich mag würde meine Matrix A durch Spalte 2 dann 3.Julia: Sortieren Matrix von Spalte 2, dann 3

A = round(randn(100,4)) 

Vielleicht etwas sortieren wie:

sort(A,(0,2:3)) 
100x4 Array{Float64,2}: 
    0.0 -2.0 -2.0 -1.0 
-1.0 -2.0 -1.0 1.0 
    1.0 -2.0 -1.0 2.0 
-1.0 -2.0 0.0 0.0 
-1.0 -2.0 0.0 -1.0 
-0.0 -2.0 0.0 -1.0 
    1.0 -2.0 0.0 0.0 
    1.0 -2.0 1.0 -1.0 
-0.0 -2.0 2.0 -1.0 
-0.0 -1.0 -2.0 1.0 
    ⋮      
-0.0 1.0 0.0 1.0 
    1.0 1.0 1.0 1.0 
    0.0 1.0 1.0 -1.0 
-0.0 1.0 2.0 0.0 
-0.0 2.0 -1.0 0.0 
-2.0 2.0 -1.0 1.0 
    2.0 2.0 -0.0 -1.0 
-1.0 2.0 -0.0 -1.0 
    1.0 2.0 0.0 2.0 
-1.0 2.0 2.0 0.0 

Antwort

18

Es gibt eine Funktion, die sortrows nimmt einen by Schlüsselwort, das Sie dies tun können:

julia> sortrows(A, by=x->(x[2],x[3])) 
100x4 Array{Float64,2}: 
    2.0 -3.0 -0.0 0.0 
-1.0 -2.0 -1.0 -1.0 
-0.0 -2.0 -0.0 0.0 
    0.0 -2.0 0.0 -1.0 
    1.0 -2.0 1.0 2.0 
-0.0 -2.0 1.0 -1.0 
-1.0 -1.0 -2.0 1.0 
-1.0 -1.0 -2.0 -0.0 
-1.0 -1.0 -1.0 1.0 
-0.0 -1.0 -1.0 0.0 
    ⋮ 
-0.0 1.0 1.0 -1.0 
-0.0 1.0 2.0 1.0 
    0.0 1.0 2.0 0.0 
-1.0 2.0 -2.0 1.0 
    0.0 2.0 -2.0 -2.0 
    1.0 2.0 -1.0 0.0 
    0.0 2.0 -1.0 -0.0 
-1.0 2.0 0.0 -1.0 
-0.0 2.0 2.0 0.0 
    1.0 3.0 2.0 1.0 

Die Sortier API ist ziemlich flexibel - Sie finden die Dokumentation here.