Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: simple question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105249] Re: simple question
  • From: Valeri Astanoff <astanoff at gmail.com>
  • Date: Wed, 25 Nov 2009 02:35:19 -0500 (EST)
  • References: <hegf08$1g1$1@smc.vnet.net>

On 24 nov, 12:11, Francisco Gutierrez <fgutiers2... at yahoo.com> 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.
>
> 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")?
> Fg

Good day,

It could be done this way :

In[1]:= orden[l1_List, l2_List, parm_List]:=
        And@@((l1[[#]] >= l2[[#]])& /@ parm);

In[2]:= gathering[l1_List, parm_List]:=
Split[Sort[l1,orden[#1,#2,parm]&],(#1[[parm]]==#2[[parm]])&];

In[3]:= 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,=AD0,1,1},{2,1,1,1,2},{0,1,1,0,1}};

In[4]:= gathering[ejemplo1,{4,5}]
Out[4]= {{{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}}}

hth

--
Valeri Astanoff


  • Prev by Date: Re: Bug in Plot3D?
  • Next by Date: Re: simple question
  • Previous by thread: Re: simple question
  • Next by thread: Re: simple question