# Permuting the rows of a matrix based on given a given permutation

**URL:** <https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444>\
**Category:** General Usage\
**Created:** [June 15, 2020, 2:52pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444 "2020-06-15T14:52:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 15, 2020, 2:52pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/1 "2020-06-15T14:52:51Z")

</div>

Suppose I am given an mXn matrix A, and I have a permutation of {1,…,m}, for example by

```julia
p = sortperm(A[:,1]);

```

I now wish to rearrange the wors of `A` based on `p`. How do I do that ? The `permute!()` function only applies to vectors, as shown [here](https://docs.julialang.org/en/v1/base/arrays/#Base.permute!-Tuple%7BAny,AbstractArray%7BT,1%7D%20where%20T%7D). I can always loop over the columns, as in

```julia
for j in 1:size(A,2)
     permute!(A[:,j], p);
end

```

but is there a better way ?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 15, 2020, 3:52pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/2 "2020-06-15T15:52:38Z")

</div>

> [@iamsuddhasattwa](#):
>
> `permute!(A[:,j], p);`

Note that this permutes a _copy_ of the column, unless you switch to using views (e.g. wrapping the code block or the line in a `@views` macro call).

For an alternative approach, you could write something similar to the [`Base.permutecols!!` function](https://github.com/JuliaLang/julia/blob/25dc696bdd378d3fdfb83c8e33db0c84ed6adde5/base/combinatorics.jl#L96-L125) (which permutes columns, not rows). (Since Julia’s `Array` is column-major, however, permuting rows with this algorithm will probably not be as efficient as permuting columns.) Or your could rearrange your code to swap dimensions on your array, and then call the `Base.permutecols!!` function directly. (It has a double `!!` because it also overwrites the permutation array `p`.)

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 15, 2020, 7:42pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/3 "2020-06-15T19:42:07Z")

</div>

> [@stevengj](#):
>
> (It has a double `!!` because it also overwrites the permutation array `p` .)

Is this a (new) convention? Is it meant to signify that more than one passed argument is being mutated, or is its meaning more specific?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 15, 2020, 7:48pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/4 "2020-06-15T19:48:28Z")

</div>

> [@anon94023334](#):
>
> Is this a (new) convention? Is it meant to signify that more than one passed argument is being mutated, or is its meaning more specific?

A couple of Base functions use it to indicate that multiple arguments are mutated, but I don’t think it’s a very widespread convention in Julia.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 15, 2020, 9:51pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/5 "2020-06-15T21:51:06Z")

</div>

You can use `sortslices`, it sorts by rows, columns, or any row/column specified by the user.

```julia
julia> A = rand(0:9,5,5)
5×5 Array{Int64,2}:
 4 6 4 6 3
 2 7 5 5 2
 7 7 0 4 3
 4 5 2 5 6
 6 5 8 1 0

julia> sortslices(A, dims=1)
5×5 Array{Int64,2}:
 2 7 5 5 2
 4 5 2 5 6
 4 6 4 6 3
 6 5 8 1 0
 7 7 0 4 3

```

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [July 2, 2020, 8:02pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/6 "2020-07-02T20:02:45Z")

</div>

Thank you. Although I was looking for an in-place algorithm, this function is a very useful to learn. From the examples that you provided, it seems that the sorting is only according to the first column or row. But what if I try to rearrange the rows in in increasing order of the second column ? You said that it can be done, could you give me the code to do it ? Julia’s [documentation](https://docs.julialang.org/en/v1/base/sort/#Base.sortslices) on sortclices does not provide a solution.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [July 2, 2020, 8:06pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/7 "2020-07-02T20:06:20Z")

</div>

Thank you, however the function permutecolums itself did not work.

```julia
julia> permutecols!!(A,p)
ERROR: UndefVarError: permutecols!! not defined
Stacktrace:
 [1] top-level scope at none:0

```

In fact, it seems that Base does not have any such function defined.

```julia
julia> Base.permute
permute! permute!! permutedims permutedims!

```

In the hyperlink that you provided, the function was defined in the Combinatorics.jl package. importing the package also did not work. I tried copying the function definition and explicitly defining `permutecolumns!!`. But this gives the error

```julia
julia> permutecols!!(A,p)
ERROR: UndefVarError: require_one_based_indexing not defined
Stacktrace:
 [1] permutecols!!(::Array{Float64,2}, ::Array{Int64,1}) at .\REPL[22]:2
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [July 2, 2020, 8:25pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/8 "2020-07-02T20:25:50Z")

</div>

Yes, as I said you can sort by any row/column you want which can be very useful. Just override the `lt` function to specify how the new comparison is done. Searching for documentation inside the REPL is so easy, just type `?sortslices` and you get good examples.

```julia
julia> A = rand(0:9,5,5)
5×5 Array{Int64,2}:
 6 5 5 0 0
 3 4 5 9 5
 0 2 5 0 5
 9 9 1 7 2
 3 1 2 4 7

julia> sortslices(A, dims=1, lt=(x,y)->isless(x[2],y[2]))
5×5 Array{Int64,2}:
 3 1 2 4 7
 0 2 5 0 5
 3 4 5 9 5
 6 5 5 0 0
 9 9 1 7 2

```

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [July 2, 2020, 8:33pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/9 "2020-07-02T20:33:26Z")

</div>

Thanks. I did search for sortslices. But there is no way anyone can possibly understand from the documentation alone that `lt=(x,y)->isless(x[2],y[2]))` is supposed to mean ``by second column".

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 2, 2020, 10:04pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/10 "2020-07-02T22:04:30Z")

</div>

> [@iamsuddhasattwa](#):
>
> In fact, it seems that Base does not have any such function defined.

It was added in Julia 1.2.

---

<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:** [July 2, 2020, 11:23pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/11 "2020-07-02T23:23:47Z")

</div>

> [@iamsuddhasattwa](#):
>
> But there is no way anyone can possibly understand from the documentation alone that `lt=(x,y)->isless(x[2],y[2]))` is supposed to mean ``by second column".

You can also write `sortslices(A, dims=1, by=row->row[2])`, although I agree it’s not super-obvious what’s going to happen.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [July 3, 2020, 2:30am UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/12 "2020-07-03T02:30:22Z")

</div>

I see

---

<div class="post-metadata">

**Author:** ![Xavier\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xavier_gonzalez/32/36033_2.png) [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)\
**Post date:** [November 20, 2022, 11:11pm UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/13 "2022-11-20T23:11:01Z")

</div>

I think this code doesn’t work b/c you haven’t specified `j`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 22, 2025, 12:08am UTC](https://discourse.julialang.org/t/permuting-the-rows-of-a-matrix-based-on-given-a-given-permutation/41444/14 "2025-05-22T00:08:26Z")

</div>

2 posts were split to a new topic: [Constructing a permutation matrix](https://discourse.julialang.org/t/constructing-a-permutation-matrix/129234)
