Julia: sort matrix columns by values ​​in another vector (in place ...)?

I'm interested in sorting matrix columns in terms of values ​​from two other vectors. As an example, suppose the matrix and vectors are as follows:

M = [ 1 2 3 4 5 6 ; 7 8 9 10 11 12 ; 13 14 15 16 17 18 ] v1 = [ 2 , 6 , 6 , 1 , 3 , 2 ] v2 = [ 3 , 1 , 2 , 7 , 9 , 1 ] 

I want to sort the columns of A in terms of their respective values ​​in v1 and v2 , with v1 precedence over v2 . Also, I am interested in sorting the matrix in place , because the matrices I work with are very large. Currently, my rude solution is as follows:

 MM = [ v1' ; v2' ; M ] ; ## concatenate the vectors with the matrix MM[:,:] = sortcols(MM , by=x->(x[1],x[2])) M[:,:] = MM[3:end,:] 

which gives the desired result:

 3x6 Array{Int64,2}: 4 6 1 5 2 3 10 12 7 11 8 9 16 18 13 17 14 15 

Obviously, my approach is not ideal, as it requires computing and storing intermediate matrices. Is there a more efficient / elegant approach for sorting matrix columns through 2 other vectors? And can it be done in place to preserve memory?

I used to use sortperm to sort an array in terms of values ​​stored in another vector. Is it possible to use sortperm with two vectors (and in place)?

+8
sorting arrays matrix julia-lang
source share
1 answer

I would do this:

 julia> cols = sort!([1:size(M,2);], by=i->(v1[i],v2[i])); julia> M[:,cols] 3Γ—6 Array{Int64,2}: 4 6 1 5 2 3 10 12 7 11 8 9 16 18 13 17 14 15 

This should be pretty fast and use only one temporary vector and one copy of the matrix. It is not completely in place, but performing this operation in place is not easy. You will need a sort function that moves the columns as it works, or alternatively the permute! version permute! which works with columns. You can start with the code for permute!! in combinatorics.jl and change it to rearrange the columns, reusing the temporary buffer with the column size.

+8
source share

All Articles