MathGroup Archive 2009

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

Search the Archive

Re: Some function like Position[] but uses criteria, not pattern?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99467] Re: [mg99389] Some function like Position[] but uses criteria, not pattern?
  • From: Curtis Osterhoudt <cfo at lanl.gov>
  • Date: Wed, 6 May 2009 05:25:03 -0400 (EDT)
  • Organization: LANL
  • References: <200905050937.FAA20364@smc.vnet.net>
  • Reply-to: cfo at lanl.gov

In[1]:= a = {1, 0, 4, -5, 8}

Out[1]= {1, 0, 4, -5, 8}

In[2]:= Position[a, 0]

Out[2]= {{2}}

In[4]:= Position[a, _?(# > 3 &)]

Out[4]= {{3}, {5}}


            Hope that helps, 
                                          C.O.



On Tuesday 05 May 2009 03:37:22 am Nasser Abbasi wrote:
> V 7.0
> 
> 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.
> 
> I can't type
> Position[a, #1 > 3 & ]
> 
> Now, Select[] does take a criteria, good, so I could type
> 
> Select[a, #1 > 3 & ]
> {4, 8}
> 
> But I wanted the positions of the elements, not the elements themselves.
> 
> I could use Select to find the elements, then use Position to find the 
> position of these elements as follows
> 
> c = Select[a, #1 > 3 & ];
> Flatten[(Position[a, #1] & ) /@ c]
> 
> Out[65]= {3, 5}
> 
> But I think this is bit messy. I think Position[] or new function similar, 
> should have an option to return position of elements based on criteria, not 
> just pattern. The above gets more messy for me, when I wanted to find 
> position of elements in a matrix based on some criteria.
> 
> For example, find the position (i.e. row and column) index in a matrix (list 
> of lists) of those elements which matches some criteria. Say >3.
> 
> thanks,
> --Nasser
> 
> 
> 
> 
> 


-- 
==================================
Curtis Osterhoudt
cfo at remove_this.lanl.and_this.gov
PGP Key ID: 0x4DCA2A10
==================================



  • Prev by Date: Re: RegionPlot3d under sampling plot points?
  • Next by Date: Re: "Sticky" options when using Plot[] -- bug or feature?
  • Previous by thread: Re: Some function like Position[] but uses criteria, not
  • Next by thread: Re: Some function like Position[] but uses criteria, not pattern?