MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Using Select with arrays? (Relative newbie)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54306] Re: [mg54258] Re: Using Select with arrays? (Relative newbie)
  • From: DrBob <drbob at bigfoot.com>
  • Date: Wed, 16 Feb 2005 14:36:45 -0500 (EST)
  • References: <200502140317.WAA14058@smc.vnet.net> <200502150250.VAA27328@smc.vnet.net> <opsl80wafhiz9bcq@monster.ma.dl.cox.net> <873831F5-D563-4249-AE56-DB483366BFDF@yale.edu>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

That works, but you're not using the Listable attribute of Max; in fact, Max isn't Listable.

Attributes@Max
{Flat,NumericFunction,OneIdentity,Orderless,Protected}

But my solution is actually bogus, in case the Max overall for the second coordinate isn't found in the subset that has 1 in the first coordinate. A more general solution is:

Last@Sort@Cases[data,{1,_,_,_,_}]

{1,4,string4,c,d}

That requires the two positions used to be first and second, as they are in this case. If we wanted 1 in the second position and maximum in the first position, it's simple again:

Last@Sort@Cases[data,{_,1,_,_,_}]

{4,1,string2,c,d}

But if the key positions are 2nd and 4th, for instance, it's more complicated. For instance:

data=RandomArray[BinomialDistribution[20,.1],{25,5}]
#[[Ordering[#[[All,4]],-1]]]&@Cases[data,{_,1,_,_,_}]

{{1,1,1,1,2},{3,2,2,0,5},{0,2,6,2,2},{3,2,3,4,3},{5,1,3,1,3},{2,2,3,1,2},{
   1,2,1,1,2},{1,1,1,3,0},{1,2,3,5,2},{2,2,2,1,3},{3,3,1,2,2},{1,0,2,1,1},{
   2,1,1,4,2},{2,3,2,2,1},{2,0,0,1,6},{1,3,2,4,2},{3,0,4,2,2},{1,0,0,0,2},{
   3,1,0,1,3},{3,4,1,1,1},{2,2,4,1,3},{1,4,1,2,2},{3,1,2,1,3},{1,2,0,2,2},{
   0,1,0,1,1}}

{{2,1,1,4,2}}

Max@data[[All,4]]
5

That's still reasonably simple, I think.

Bobby

On Tue, 15 Feb 2005 13:50:05 -0500, János <janos.lobb at yale.edu> wrote:

> Bobby,
>
> Elegant and fast.  I added to my "pearls collection" of mathgroup
> contributions.
>
> I see that I could have used the Listable attribute of Max.  So, here
> is a shorter version, still just with Select.
>
> In[25]:=
> Select[data,
>    #1[[2]] == Max[data[[All,
>         2]]] && #1[[1]] ==
>       1 & ]
> Out[25]=
> {{1, 4, string4, c, d}}
>
> Thanks a lot,
>
> János
> P.S.  Here is another way with Pick and Map --not as elegant.
>
> In[52]:=
> Pick[data,
>    (#1[[2]] == Max[data[[All,
>          2]]] && #1[[1]] ==
>        1 & ) /@ data]
> Out[52]=
> {{1, 4, string4, c, d}}
>
> On Feb 15, 2005, at 12:15 PM, DrBob wrote:
>
>> data = {{1, 1, string1, c, d},
>>     {1, 2, string2, c, d},
>>     {1, 3, string3, c, d},
>>     {1, 4, string4, c, d},
>>     {2, 1, string1, c, d},
>>     {2, 2, string2, c, d},
>>     {3, 1, string3, c, d},
>>     {4, 1, string2, c, d},
>>     {4, 2, string4, c, d}};
>> Cases[data, {1, Max[data[[All,2]]], _, _, _}]
>> {{1, 4, string4, c, d}}
>> Bobby
>> On Mon, 14 Feb 2005 21:50:53 -0500 (EST), János <janos.lobb at yale.edu>
>> wrote:
>>> Well, I did not read to the end at first.
>>> If you want to use Select to select the element which has maximum in
>>> its #2 location from all those who have 1 in their #1 location, then
>>> you can do something like:
>>> In[23]:=
>>> Select[Select[data,
>>>     #1[[1]] == 1 & ],
>>>    #1[[2]] == Max[
>>>       Select[data,
>>>         #1[[1]] == 1 & ][[All,
>>>        2]]] & ]
>>> Out[23]=
>>> {{1, 4, string4, c, d}}
>>> János
>>> On Feb 13, 2005, at 10:17 PM, Hugo Mallinson wrote:
>>>> The subject might not be entirely correct, but here is my problem:
>>>> I have a list of 5-variable data points like
>>>> data = {
>>>> {1, 1, string1, c, d}
>>>> {1, 2, string2, c, d}
>>>> {1, 3, string3, c, d}
>>>> {1, 4, string4, c, d}
>>>> {2, 1, string1, c, d}
>>>> {2, 2, string2, c, d}
>>>> {3, 1, string3, c, d}
>>>> {4, 1, string2, c, d}
>>>> {4, 2, string4, c, d}
>>>> }
>>>> and I want to extract just the points that have 1 (or 2, etc) as
>>>> their
>>>> first value. I think I should do something like
>>>> Select[data, {1, _Integer, _String, _Integer, _Integer}]
>>>> but that doesn't work.
>>>> Having done that I need to find the maximum value of #2 for each
>>>> string, which I presumably do by the same method as above to extract
>>>> all string1 (or ...2) and then use Map[] and Max[]. I would do this
>>>> all
>>>> with For loops (revealing my lack of Mathematica chops :-) ) but I'd
>>>> really like to learn how to do this sort of extraction in a
>>>> Mathematica-y way. Any help would be greatly appreciated!
>>>> Hugo
>>> ----------------------------------------------
>>> Trying to argue with a politician is like lifting up the head of a
>>> corpse.
>>> (S. Lem: His Master Voice)
>> --
>> DrBob at bigfoot.com
>> www.eclecticdreams.net
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Re: Using Select with arrays? (Relative newbie)
  • Next by Date: IMS 2005: Second announcement and call for submissions
  • Previous by thread: Re: Re: Using Select with arrays? (Relative newbie)
  • Next by thread: Re: Re: Using Select with arrays? (Relative newbie)