I want to use the command Permute to arrange the rows of a matrix. What I am looking to do is given a list I want to generate a new list which gives me the positions of the elements of the first list according to their values. That is, if there is a list $\{p_1,p_2,p_3,p_4\}$ such that $p_2>p_3>p_4>p_1$ then I would want a list $\{4,1,2,3\}$. For example: I have a list $\{2,1,3,0\}$ which I want to convert into $\{2,3,1,4\}$ so that I could arrange the rows of the matrix according to the latter list.
1 Answer
$\begingroup$
$\endgroup$
1
It seems that you want:
- Decreasing order, not increasing. We need
Reverse
. - The inverse permutation of the result of
Ordering
. We need an extraOrdering
to invert it.
a = {2, 1, 3, 0}
(* {2, 1, 3, 0} *)
result = Ordering@Reverse@Ordering[a]
(* {2, 3, 1, 4} *)
The question would have been much easier to understand if it is also stated in precise terms rather than just an example. For example, you could have said that you needed
ReverseSort[a][[result]] === a
-
$\begingroup$ Thank you. It works. @Szabolcs $\endgroup$ Commented Sep 20, 2018 at 9:22
Ordering
:Reverse@Ordering[{2, 1, 3, 0}]
is{3, 1, 2, 4}
. $\endgroup$