Mathematica 9 is now available
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: [mg95869] Re: [mg95855] Conditional list indexing
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 29 Jan 2009 05:51:35 -0500 (EST)
  • References: <11575212.1233143165868.JavaMail.root@m02>

Check out MapIndexed.

a = Table[Prime[i], {i, 10}]
{2,3,5,7,11,13,17,19,23,29}

Here is MapIndexed with a generic function f.

MapIndexed[f, a]
{f[2, {1}], f[3, {2}], f[5, {3}], f[7, {4}], f[11, {5}], f[13, {6}], 
 f[17, {7}], f[19, {8}], f[23, {9}], f[29, {10}]}

We want f to be a function that will do the test on the first element, if it
passes return the first part of the second element, otherwise return a blank
Sequence. The blank Sequence must be unevaluated so it is not eliminated.

MapIndexed[If[#1 > 15, First[#2], Unevaluated@Sequence[]] &, a]
{7, 8, 9, 10}

Look up Sequence. Notice that:

If[x, y, Sequence[]]    gives
If[x, y]			   not what we want,	

but 

If[x, y, Unevaluated@Sequence[]]  works properly.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: daniele [mailto:danielelinaro at gmail.com] 


Hello everybody,
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.

Thank you,
Daniele





  • Prev by Date: O in Mathematica
  • Next by Date: Re: moment generating function for gaussian and lognormal
  • Previous by thread: Re: Conditional list indexing
  • Next by thread: Mathieu equation