Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Conditional list indexing

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95868] Re: Conditional list indexing
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 29 Jan 2009 05:51:24 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <glpgd4$lha$1@smc.vnet.net>

In article <glpgd4$lha$1 at smc.vnet.net>,
 daniele <danielelinaro at gmail.com> wrote:

> I'd like to know how I can get the indices of the elements in a list
> that satisfy a certain condition. Here's an example: suppose I have
> the list
> 
> a = Table[Prime[i],{i,10}];
> 
> that contains {2,3,5,7,11,13,17,19,23,29}.
> 
> If I execute the command
> 
> b = Select[a,#>15&];
> 
> b will contain {17,19,23,29}. I'd like to find a way to obtain a third
> list, say c, that will contain {7,8,9,10}, i.e. the indices of the
> elements in list a that are greater than 15.

Use *Position* to get the "indices".

In[1]:= a = Table[Prime[i], {i, 10}]

Out[1]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}

In[2]:= b = Select[a, # > 15 &]

Out[2]= {17, 19, 23, 29}

In[3]:= c = Position[a, p_?(# > 15 &)]

Out[3]= {{7}, {8}, {9}, {10}}

In[4]:= c = Position[a, p_ /; p > 15]

Out[4]= {{7}, {8}, {9}, {10}}

In[5]:= c = Flatten[Position[a, p_ /; p > 15]]

Out[5]= {7, 8, 9, 10}

Regards,
--Jean-Marc


  • Prev by Date: Re: Derivative
  • Next by Date: Re: Derivative
  • Previous by thread: Re: Conditional list indexing
  • Next by thread: Re: Conditional list indexing