Hi Mario,
You can use Position to get the position of each element in the ordered version. Note that elements equal in the determination of the ordering function tie for position.
Best,
David
(* a list *)
list = {1, 2, 4, 2, 3};
(* ordered *)
ordered = Ordering[list]
{1, 2, 4, 5, 3}
(* the position which each thing in the list occupies in the odered \
list *)
Position[ordered, #] & /@ list // Flatten
{1, 2, 3, 2, 5}
Oops!
You’re quite right, Mario. The code below corrects the problem. You can see in the output temp the element positions are each a list of the positions in which the element ranks, with more than one if needed.. The final line just assigns an element the rank of its first occurence. Either could esily be turned into a function. Also, duplicated elements do not reduce the rank of followers, so we get {1,2,5,2,4}.
Best,
David
(*a list*)
list = {1, 2, 4, 2, 3}
(* {1,2,4,2,3} *)
(* sorted list *)
sorted = Sort[list]
(* {1,2,2,3,4} *)
temp = Position[sorted, #] & /@ list
(* {{{1}},{{2},{3}},{{5}},{{2},{3}},{{4}}} *)
(*the position which each thing in the list occupies in the ordered \
list*)
First /@ temp // Flatten
(* {1,2,5,2,4} *)
Awesome. Thank you so much. I had some more work to do do generalize this for my application, but I winged it (in an educated guess sort of way) and it worked!
One thing I have to work on is understanding the shortcuts. I spend way too much time trying to figure out what the //, /@ and #$%& mean.
You’re welcome, Mario.
Each of the brief forms has a longer form, and it would have been kinder of me to stick to the more straitforward syntax.
// is a postfix form for a function: x//Flatten is equivalent to Flatten;
/@ is a short form for Map at the first level. f/@list is equivalent to Map[f,list].
And the Position[sorted, #] & defines a pure function where # represents the argument and & declares the preceding to be a pure function definition.
It could have been defined separate from its use as pos:=Position[sorted,x], and then pos mapped onto the list just as the pure function was.