1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ I do not understand what is "the positions of the elements of the first list according to their values". $\endgroup$
    – Szabolcs
    Commented Sep 20, 2018 at 8:13
  • $\begingroup$ It means if there is a list $\{p1,p2,p3,p4\}$ such that $p2>p3>p4>p1$ then I would want a list $\{4,1,2,3\}$. @Szabolcs $\endgroup$ Commented Sep 20, 2018 at 8:18
  • $\begingroup$ Maybe you are looking for Ordering: Reverse@Ordering[{2, 1, 3, 0}] is {3, 1, 2, 4}. $\endgroup$ Commented Sep 20, 2018 at 8:19
  • $\begingroup$ Please edit clarifications into the question instead of adding them in comments. $\endgroup$
    – Szabolcs
    Commented Sep 20, 2018 at 8:20

1 Answer 1

2
$\begingroup$

It seems that you want:

  • Decreasing order, not increasing. We need Reverse.
  • The inverse permutation of the result of Ordering. We need an extra Ordering 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
$\endgroup$
1
  • $\begingroup$ Thank you. It works. @Szabolcs $\endgroup$ Commented Sep 20, 2018 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.