Ordering Of Matrix Columns

Hi,

I have a matrix of size 9x100

9×100 Matrix{Float64}:
 1.08563     1.20562     1.26562     1.14562     1.17562     1.29562     1.23562     1.11563     1.13062     …  1.15031     1.27031     1.21031     1.09031     1.09406     1.21406     1.27406     1.15406
 2.44922     2.69922     2.32422     2.57422     2.26172     2.51172     2.38672     2.63672     2.35547        2.30664     2.55664     2.43164     2.68164     2.2832      2.5332      2.4082      2.6582
 1.06406     0.964063    1.01406     0.914062    0.989063    1.08906     0.939063    1.03906     0.926563       0.977344    1.07734     0.927344    1.02734     1.09922     0.999219    1.04922     0.949219
 0.440215    0.398965    0.41959     0.37834     0.388652    0.429902    0.409277    0.450527    0.424746       0.37415     0.4154      0.394775    0.436025    0.432158    0.390908    0.452783    0.411533
 0.579248    0.522993    0.551121    0.607376    0.593312    0.537057    0.50893     0.565184    0.530025       0.548044    0.604299    0.576172    0.519917    0.591993    0.535739    0.507611    0.563866
 0.286828    0.315828    0.301328    0.272328    0.279578    0.308578    0.294078    0.265078    0.268703    …  0.269383    0.298383    0.312883    0.283883    0.275727    0.304727    0.290227    0.261227
 1.34266     1.21266     1.40766     1.27766     1.37516     1.24516     1.31016     1.18016     1.39141        1.24008     1.37008     1.17508     1.30508     1.18727     1.31727     1.25227     1.38227
 0.00376875  0.00416875  0.00436875  0.00396875  0.00426875  0.00386875  0.00366875  0.00406875  0.00421875     0.00437187  0.00397187  0.00377188  0.00417188  0.00405938  0.00365937  0.00385937  0.00425938
 0.104844    0.0948438   0.0998438   0.109844    0.0973438   0.107344    0.102344    0.0923438   0.0960938      0.0989844   0.108984    0.103984    0.0939844   0.108047    0.0980469   0.0930469   0.103047

Each one of the column vectors represents a specific point in parameter space. I would like to order these columns from low to high while preserving the order at which the elements appear in the column. I might have missed something in the documentation I thought best to ask :slight_smile:

Cheers

What do you mean by “from low to high” when talking about columns?

Good point I am not overly clear here my fault! :slight_smile:

I would like to do something along the lines of order each column based on the size of its magnitude then order the columns while preserving element order.

What do you mean by “size of the magnitude” of a column element? And according to which criterion would you then “order the columns”?

it sounds like you want to check out eachcol, which you can then sort. that said, you might want to use a vector of vectors (or vector of staticvectors) instead

so using

|a|=sqrt(x^2+y^2+..)

to calculate the magnitude of the column vector then order the columns going from the lowest magnitude column vector to the highest magnitude.

while preserving the position of each element in the column vector :slight_smile:

I will check that out thanks! :slight_smile:

A[:, sortperm(norm.(eachcol(A)))]

will return a copy of A with columns ordered wrt. their 2-norm.

Definitely use sortslices as shown below!

Thank you!

You can also sort the columns and re-assemble:

julia> A = randn(2,5);

julia> reduce(hcat, sort(eachcol(A); by=norm))
2×5 Matrix{Float64}:
 -0.111708  -1.31249   -1.49302    1.11223   0.730328
  0.684414   0.589954  -0.115788  -1.068    -1.93679

julia> ans == A[:, sortperm(norm.(eachcol(A)))]
true

Edit: or better, use sortslices which takes the same keyword:

julia> sortslices(A, dims=2, by=norm)
2×5 Matrix{Float64}:
 -0.111708  -1.31249   -1.49302    1.11223   0.730328
  0.684414   0.589954  -0.115788  -1.068    -1.93679
4 Likes