MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sorting question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52769] Re: [mg52758] Sorting question
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Mon, 13 Dec 2004 04:22:06 -0500 (EST)
  • References: <200412111023.FAA06589@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 11 Dec 2004, at 19:23, Nimrod wrote:

>
>  A simple sorting question that has me stumped as to how to do it both
> quickly and elegantly.
>
> I have a list of data pairs of the form (distance, time).  I want to
> sort on the distance element and then remove all pairs where the time
> element is out of order with the distance element.  For example
> ((1,1),(2,3),(3,2),(4,4)) --> ((1,1),(4,4)).  The actual lists are
> much longer.
>
> Thanks in advnace,
>
> Nimrod.
>
>
I am not quite sure I understand you correctly, but here is one 
possible way. (If I understand you correctly it does not matter whether 
you sort on space or time, the final result should be the same).

F[ls_] := With[{w =
   Transpose[ls]}, Extract[Sort[ls], Position[Ordering[
     First[w]] - Ordering[Last[w]], 0]]]

For example if the list is:

ls={{0,3},{0.8,20}, {0.3,5},{0.9,17},{10,100}};

then

F[ls]

{{0, 3}, {0.3, 5}, {10, 100}}


This can many pretty large lists:


ls=Table[With[{x=Random[Real,{0,20}]},{x,10 
x+Random[Real,{-1,1}]}],{20000}];


Timing[Length[F[ls]]]


{0.1600000000000037*Second, 100}


Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/~andrzej/
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: Q:Changing Mathematica default terms arrangement
  • Next by Date: Re: multiple outputs from a function
  • Previous by thread: Re: Sorting question
  • Next by thread: Re: Sorting question