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

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. I can always loop over the columns, as in

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 (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.)

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 on sortclices does not provide a solution.

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

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.

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".