Re: Finding position of an element in a list:
- To: mathgroup at smc.vnet.net
- Subject: [mg84029] Re: [mg83986] Finding position of an element in a list:
- From: Curtis Osterhoudt <cfo at lanl.gov>
- Date: Fri, 7 Dec 2007 03:04:20 -0500 (EST)
- Organization: LANL
- References: <200712060743.CAA10228@smc.vnet.net>
- Reply-to: cfo at lanl.gov
Hi, Gopinath, The problem is that dropOuterBraces is turning the list of values returned by Nearest into strings. Then Position isn't able to determine where these strings appear in the original list -- because they don't! My recommendation is something like the following: * Use Nearest to grab the element(s) closest to what you want; * Use Position directly on those elements (not their string representations; * Flatten the result (if your original list is one-dimensional). It's basically exactly what you did, except for the step of turning things into strings. Here's a one-liner to do this: In[27]:= Flatten@(Position[xlis, #] & /@ Nearest[xlis, 6]) Out[27]= {7} Hope that helps! C.O. On Thursday 06 December 2007 00:43:15 Gopinath Venkatesan wrote: > Hello Friends, > > (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] > > As you see, the last but one command returns nothing, and we have to > explicitly say the number to return the position. > > Or should some other command be used to extract the position of an > arbitrary number in a list? > > Same with the case of MemberQ command, it gives True for some and False for > some. For example, consider this list, > > xlis = Table[N[((1 - Cos[(j - 1)/9 \[Pi]])/2) 1.32], {j, 1, 10}]; > dropOuterBraces[lst_List] := > StringTake[#1, {2, StringLength[#1] - 1}] &[ToString[lst]] > > eval = Nearest[xlis, 0.32]; > xlis > elem = dropOuterBraces[eval] > MemberQ[xlis, 0.545392] > MemberQ[xlis, 0.33] > Position[xlis, 0.33] > Position[xlis, elem] > > Please hint where I am wrong. > > Thanks, > > Gopinath Venkatesan > Graduate Student > University of Oklahoma -- ========================================================== Curtis Osterhoudt cfo at remove_this.lanl.and_this.gov PGP Key ID: 0x4DCA2A10 Please avoid sending me Word or PowerPoint attachments See http://www.gnu.org/philosophy/no-word-attachments.html ==========================================================
- References:
- Finding position of an element in a list:
- From: Gopinath Venkatesan <gopinathv@ou.edu>
- Finding position of an element in a list: