MathGroup Archive 2009

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

Search the Archive

Re: Conditional list indexing

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95921] Re: Conditional list indexing
  • From: Steven Siew <stevensiew2 at gmail.com>
  • Date: Thu, 29 Jan 2009 06:01:07 -0500 (EST)
  • References: <glpgd4$lha$1@smc.vnet.net>

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

In[2]:=
SelectIndex[list_List,func_]:=Module[{k,result={}},
    For[k=1,k <= Length[list],k++,
      If[  func[  list[[k]]  ]  ,
          result=Append[result,k]  ];
      ];
    result
    ]

In[3]:=
SelectIndex[a,#>15&]
Out[3]=
{7,8,9,10}

A much better solution would be as follows

In[4]:=
SelectIndex2[list_List,func_]:=Flatten@Position[Map[func,list],True]

In[5]:=
SelectIndex2[a,#>15&]
Out[5]=
{7,8,9,10}


  • Prev by Date: Re: Looping
  • Next by Date: Re: Compute ArcTan(n Pi+ Pi/2 + O(1))
  • Previous by thread: Conditional list indexing
  • Next by thread: Re: Conditional list indexing