# Ordering Of Matrix Columns

**URL:** <https://discourse.julialang.org/t/ordering-of-matrix-columns/81917>\
**Category:** General Usage\
**Tags:** sort, matrices\
**Created:** [May 30, 2022, 2:44pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917 "2022-05-30T14:44:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [May 30, 2022, 2:44pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/1 "2022-05-30T14:44:02Z")

</div>

Hi,

I have a matrix of size 9x100

```julia
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 🙂

Cheers

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 30, 2022, 2:45pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/2 "2022-05-30T14:45:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [May 30, 2022, 2:50pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/3 "2022-05-30T14:50:57Z")

</div>

Good point I am not overly clear here my fault! 🙂

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.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 30, 2022, 2:52pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/4 "2022-05-30T14:52:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 30, 2022, 2:56pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/5 "2022-05-30T14:56:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [May 30, 2022, 2:57pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/6 "2022-05-30T14:57:44Z")

</div>

so using

```julia
|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 🙂

---

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [May 30, 2022, 2:58pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/7 "2022-05-30T14:58:05Z")

</div>

I will check that out thanks! 🙂

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 30, 2022, 2:58pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/8 "2022-05-30T14:58:27Z")

</div>

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

```

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

Definitely use `sortslices` as shown below!

---

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [May 30, 2022, 3:16pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/9 "2022-05-30T15:16:29Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 30, 2022, 3:17pm UTC](https://discourse.julialang.org/t/ordering-of-matrix-columns/81917/10 "2022-05-30T15:17:10Z")

</div>

You can also sort the columns and re-assemble:

```julia
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
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

```
