MathGroup Archive 2009

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

Search the Archive

Re: simple question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105226] Re: simple question
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Wed, 25 Nov 2009 02:30:44 -0500 (EST)

On 11/24/09 at 5:48 AM, fgutiers2002 at yahoo.com (Francisco Gutierrez)
wrote:

>Dear List: I have the following list:
>ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1
>},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}}; I want to group it, so that
>the sublists of ejemplo1 that have identical values at positions 4
>and 5 are gathered together. So I did the following code: Split[
>Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]]
>&],Take[#1,{4,5}]==Take[#2,{4,5}]&]

>Works! The output in effect is:
>{{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{
>{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}}, precisely what I wanted.

Using version 7 of Mathematica, you can achieve the same
grouping using GatherBy although the resulting groups will be
returned in a different order. Given positions 4 and 5 are the
last two positions

GatherBy[ejemplo1,#[[-2;;]]&]

results in the desired grouping which is confirmed by

In[7]:= Sort[
   Split[Sort[ejemplo1, #1[[4]] >= #2[[4]] && #1[[5]] >= #2[[5]] &],
    Take[#1, {4, 5}] == Take[#2, {4, 5}] &]] ==
  Sort[GatherBy[ejemplo1, #[[-2 ;;]] &]]

Out[7]= True

>Now, how can I create a function for the general case (instead of
>fixed positions 4 and 5, an arbitrary number of positions that act
>as "gathering parameter")?

If the positions to use as the "gathering parameter" are always
the last n, then the following will work

group[data_,n_]:=GatherBy[data,#[[-n;;]]&]



  • Prev by Date: Re: simple question
  • Next by Date: Re: Not all points plot on my graph...
  • Previous by thread: Re: simple question
  • Next by thread: Re: simple question