Re: Finding position of an element in a list:
- To: mathgroup at smc.vnet.net
- Subject: [mg84039] Re: Finding position of an element in a list:
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Fri, 7 Dec 2007 03:09:35 -0500 (EST)
On 12/6/07 at 2:43 AM, gopinathv at ou.edu (Gopinath Venkatesan) wrote: >(This is an easy problem, but I was unable to get it work in >Mathematica, probably because of the precision - I tried setting >precision too, still got stuck) > >Suppose we have a list, xlis, having some elements, and I have to >find one nearest value I am interested in that list. In the below >example, I wanted to find the position of a number belonging to list >and close to 6 (which is 5.5 located at 7th). >I can do this: >xlis = {0, 1, 3, 10, 4, 5, 5.5, 10.25}; >dropOuterBraces[lst_List] := StringTake[#1, {2, >StringLength[#1] - 1}] &[ >ToString[lst]]; (* took from previous help posted by Peter Pein *) >n1 = Nearest[xlis, 6] >eval = dropOuterBraces[n1] >Position[xlis, eval] >Position[xlis, 5] A one line solution to returning the desired position number is Ordering[Abs[xlis-6],1][[1]] -- To reply via email subtract one hundred and four