Re: finding positions of elements in a list

• To: mathgroup at smc.vnet.net
• Subject: [mg87074] Re: finding positions of elements in a list
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Mon, 31 Mar 2008 02:03:19 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <frt5e9\$srg\$1@smc.vnet.net>

```PeterM wrote:

> Having a few problems here with what I think should be a simple operation. I have a list as shown:
>
> mylist={{20, 109}, {20, 110}, {20, 111}, {21, 105}, {21, 106}, {21, 107},...}
>
> It's a list of {x,y} co-ordinates of some data points I'm analysing.
>
> I want to create a new list which contains only those data points whose y co-ordinate is greater than 80 ie. mylist[[n,2]]>80.
>
> Problem is, I can't work out how to use Select to do this when each element in the list has an x and a y co-ordinate. If I split the list into x and y co-ordinates and searched for those y values greater than 80, I would have a list of those y-values but would not know the corresponding x-values.
>
> I also tried Position but it wasn't clear to me if a pattern can be used for this.

FWIW,

Here is a summary of most of the post with some new solutions and some
benchmarking to get you inspired by the versatility of Mathematica
programming language.

In[1]:= myList = {{20, 109}, {20, 110}, {20, 111}, {21, 105}, {21,
106}, {21, 107}}
myList /. {x_, y_ /; y <= 107} -> Sequence[]
Cases[myList, {x_, y_ /; y > 107}]
DeleteCases[myList, {x_, y_ /; y <= 107}]
Select[myList, Last@# > 107 &]
myList[[Flatten@Position[myList, {_, y_ /; y > 107}]]]
Table[If[myList[[j]][[2]] > 107, myList[[j]]], {j,
Length[myList]}] /. Null -> Sequence[]
If[#[[2]] > 107, #] & /@ myList /. Null -> Sequence[]

Out[1]= {{20, 109}, {20, 110}, {20, 111}, {21, 105}, {21, 106}, {21,
107}}

Out[2]= {{20, 109}, {20, 110}, {20, 111}}

Out[3]= {{20, 109}, {20, 110}, {20, 111}}

Out[4]= {{20, 109}, {20, 110}, {20, 111}}

Out[5]= {{20, 109}, {20, 110}, {20, 111}}

Out[6]= {{20, 109}, {20, 110}, {20, 111}}

Out[7]= {{20, 109}, {20, 110}, {20, 111}}

Out[8]= {{20, 109}, {20, 110}, {20, 111}}

Out[9]= {{20, 109}, {20, 110}, {20, 111}}

In[10]:= myList = Table[Sequence @@ myList, {20000}];
myList /. {x_, y_ /; y <= 107} -> Sequence[] // Timing // First
Cases[myList, {x_, y_ /; y > 107}] // Timing // First
DeleteCases[myList, {x_, y_ /; y <= 107}] // Timing // First
Select[myList, Last@# > 107 &] // Timing // First
Pick[myList, Thread[myList[[All, 2]] > 107]] // Timing // First
myList[[Flatten@Position[myList, {_, y_ /; y > 107}]]] //
Timing // First
Table[If[myList[[j]][[2]] > 107, myList[[j]]], {j,
Length[myList]}] /. Null -> Sequence[] // Timing // First
If[#[[2]] > 107, #] & /@ myList /. Null -> Sequence[] //
Timing // First

Out[11]= 0.147246

Out[12]= 0.098208

Out[13]= 0.113085

Out[14]= 0.15736

Out[15]= 0.084279

Out[16]= 0.230995

Out[17]= 0.342529

Out[18]= 0.295696

In[19]:= \$Version

Out[19]= "6.0 for Mac OS X x86 (64-bit) (February 7, 2008)"

Regards,
--
Jean-Marc

```

• Prev by Date: RE: Re: Operate on Heads
• Next by Date: Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
• Previous by thread: Re: finding positions of elements in a list
• Next by thread: Re: Creating a function that is a derivative of another