Re: Some function like Positionbut uses criteria, not pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg99483] Re: Some function like Positionbut uses criteria, not pattern?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 6 May 2009 05:28:03 -0400 (EDT)
On 5/5/09 at 5:37 AM, nma at 12000.org (Nasser Abbasi) wrote:
>Position[] works by returning position of elements which matches a
>pattern. This is all good and well.
>But sometimes, I need to find the positions (i.e index) of elements
>that matches a criteria, not a pattern.
>For example, using pattern
>a = {1, 0, 4, -5, 8}
>Position[a, 0]
>Out[15]= {{2}}
>But what if I wanted the position of the elements which, say, are
>larger than 3? this is not a pttern, but a criteria. Which in this
>example should find elements which are in position 3 and 5 in list
>'a' above.
Here is one way to accomplish what you want
In[5]:= Pick[Range[Length@a], a, _?(# > 3 &)]
Out[5]= {3,5}
Alternatively, you could use Position as follows:
In[6]:= Position[a, _?(# > 3 &)]
Out[6]= {{3,5}}