Message Boards Message Boards

0
|
1917 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Sorting and ordering

Posted 11 years ago
Help!

I understand that: 

Ordering

    gives me the position of the values in list in ascending size order.
    Input:   Ordering[{1, 4, 2, 3}]
    Output:     {1,3,4,2}

    What I am looking for is a way to report back what the rank of the value in a given position is,
    Input:    f[{1,4,2,3}]
    Output:    {4,2,1,3}

    So a ranking basically.

    Any ideas of what "f" could be, or some other technique?

    Thanks in advance.

    Mario
POSTED BY: Mario Montoya
5 Replies
Posted 11 years ago
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.   

(the last one was a joke!)

Thanks again.

Mario
POSTED BY: Mario Montoya
Posted 11 years ago
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[x_]:=Position[sorted,x], and then pos mapped onto the list just as the pure function was.

Kind regards,
David
POSTED BY: David Keith
Posted 11 years ago
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} *)
POSTED BY: David Keith
Posted 11 years ago
In your example, I would be trying to get output:

{1 , 2, 5, 2, 3}  so this doesn't quite get there.
POSTED BY: Mario Montoya
Posted 11 years ago
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}
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract