[Date Index]
[Thread Index]
[Author Index]
Re: Finding position of an element in a list:
*To*: mathgroup at smc.vnet.net
*Subject*: [mg84068] Re: Finding position of an element in a list:
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Sat, 8 Dec 2007 05:48:05 -0500 (EST)
*Organization*: The Open University, Milton Keynes, UK
*References*: <fj8pfv$j3j$1@smc.vnet.net> <fjavso$s11$1@smc.vnet.net>
Kevin J. McCann wrote:
> Nice one, Bob. Yet another new function (Nearest) that I didn't know about.
>
> I found the output of your function to be a bit cumbersome. Also, if
> there are repeated nearest values, then you get the positions twice. I
> modified your function by adding a Union and a final Flatten to produce
> a single list:
>
> nearestPosition2[lst_List,val_?NumericQ]:=
> Flatten[Union[Flatten[Position[lst,#]]&/@Nearest[lst,val]]];
>
>
> So that the output of
>
> xlis = {0, 1, 5.5, 10, 4, 5, 5.5, 10.25};
>
> is {3,7} instead of {{{3},{7}},{{3},{7}}}.
>
> Cheers,
>
> Kevin
We can even slightly improve this improved function by removing the
inner Flatten as in,
nearestPosition3[lst_List, val_?NumericQ] :=
Flatten[Union[Position[lst, #] & /@ Nearest[lst, val]]];
xlis = {0, 1, 5.5, 10, 4, 5, 5.5, 10.25};
nearestPosition3[xlis, 6]
returns {3, 7}
Just my $0.02 :-)
Best regards,
Jean-Marc
> Bob Hanlon wrote:
>> nearestPosition[xlis_List, val_?NumericQ] :=
>> Flatten[Position[xlis, #]] & /@
>> Nearest[xlis, val];
>>
>> xlis = {0, 1, 3, 10, 4, 5, 5.5, 10.25};
>>
>> nearestPosition[xlis, 6]
>>
>> {{7}}
>>
>> xlis = {0, 1, 6.5, 10, 4, 5, 5.5, 10.25};
>>
>> nearestPosition[xlis, 6]
>>
>> {{3}, {7}}
>>
>>
>> Bob Hanlon
>>
>> ---- Gopinath Venkatesan <gopinathv at ou.edu> 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
>>>
>>
>
Prev by Date:
**Re: Exact roots of transcedental functions**
Next by Date:
**Re: Re: Presentations Package Announcement**
Previous by thread:
**Re: Finding position of an element in a list:**
Next by thread:
**ListPLot for 2 List**
| |