Re: Maximum in a list
- To: mathgroup at smc.vnet.net
- Subject: [mg97261] Re: [mg97209] Maximum in a list
- From: Adriano Pascoletti <adriano.pascoletti at dimi.uniud.it>
- Date: Mon, 9 Mar 2009 01:07:28 -0500 (EST)
- References: <200903081050.FAA21521@smc.vnet.net>
The problem can be solved in several ways; three of them follow.
Solution 1: based on Ordering and Part
Find the position of a maximizer
In[1]:= mylist = {{1, 2}, {3, 4}, {8, 3}}; Ordering[mylist, -1,
Last[#1] <= Last[#2] & ]
Out[1]= {2}
then get its value
In[2]:= mylist = {{1, 2}, {3, 4}, {8, 3}}; (mylist[[#1]] & )[
Ordering[mylist, -1, Last[#1] <= Last[#2] & ][[1]]]
Out[2]= {3, 4}
Solution 2: based on Sort[list, crit]
In[3]:= Sort[mylist, Last[#1] <= Last[#2] &][[-1]]
Out[3]= {3, 4}
Solution 3: optimal (linear-time)
In[5]:= Fold[If[Last[#1] < Last[#2], #2, #1] & , First[mylist],
Rest[mylist]]
Out[5]= {3, 4}
Adriano Pascoletti
2009/3/8 pacotomi <pacotomi at orange.fr>
> Hi,
> Suppose a list
>
> mylist={{1,2},{3,4},{8,3}}
>
> I would like to extract the element {i,j} of mylist for which j is max
> (here, it is the second one {3,4}).
>
> Somebody could help me, please?
>
> Pacotomi
>
- References:
- Maximum in a list
- From: pacotomi@orange.fr (pacotomi)
- Maximum in a list