Re: How to find the index of a maximal element in a list?
- To: mathgroup at smc.vnet.net
- Subject: [mg72985] Re: How to find the index of a maximal element in a list?
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sat, 27 Jan 2007 06:29:46 -0500 (EST)
On 1/25/07 at 6:19 AM, vsorana at yahoo.com (Valter Sorana) wrote:
>I may have a mental block, but I cannot find anything better than
>Position[listName,Max[listName]]
>that traverses the list twice - once to find the maximum and once to
>find where it is.
>Isn't there a way to get both the index and the max value in one go?
>(of course one could write a loop that does this, but I want to
>avoid loops)
Assuming you are using the current version of Mathematica (or at
least version 4.1 and later) the function you want to use is
Ordering, i.e.
In[9]:=
data = Table[Random[], {100}];
Ordering[data, -1]
Out[10]=
{47}
In[11]:=
Position[data, Max[data]]
Out[11]=
{{47}}
--
To reply via email subtract one hundred and four