Re: Select x s.t. y>10
- To: mathgroup at smc.vnet.net
- Subject: [mg13982] Re: [mg13957] Select x s.t. y>10
- From: BobHanlon at aol.com
- Date: Sat, 12 Sep 1998 16:59:07 -0400
- Sender: owner-wri-mathgroup at wolfram.com
data = Table[{Random[], 10 Random[]}, {1000}];
To select the first x for which the corresponding y is greater than
five:
Select[data, #[[2]]>5&][[1, 1]]//Timing
{0.0666667 Second,0.190228}
Cases[data, {x_, y_/;y>5}][[1, 1]]//Timing
{0.0666667 Second,0.190228}
Cases[data, {x_, y_/;y>5}->x][[1]]//Timing
{0.0666667 Second,0.190228}
Cases[data, {x_, y_}?(#[[2]]>5&)][[1, 1]]//Timing
{0.0833333 Second,0.190228}
Cases[data, {x_, y_}?(#[[2]]>5&)->x][[1]]//Timing
{0.116667 Second,0.190228}
firstCase[theList_List, gtr_?NumericQ] :=
Module[{selected},
selected = Cases[theList, {x_, y_/;y>gtr}];
If[Length[selected] > 0, selected[[1, 1]], {}]]
firstCase[data, 5]
0.190228
Bob Hanlon
In a message dated 9/11/98 6:47:45 PM, jgill at vbimail.champlain.edu
wrote:
>I came across what seems like it should be a simple little function, but
>could not come up with a clever way to implement it. Generally, what I
>am trying to do is take a list of lists of length 2 ie a list of x,y
>coordinates, and Select x values based on some criterion for y. For
>example I want to select the first x value for which the corresponding
>y is greater than 5.
>I can write a function using Position and Take etc. , but there must be
>a clever,and more efficient way to do this. Any thoughts... Jason