Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: combinations of n objects 2 at a time

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15215] Re: [mg15188] combinations of n objects 2 at a time
  • From: Des Penny <penny at suu.edu>
  • Date: Tue, 22 Dec 1998 04:01:45 -0500
  • Organization: Southern Utah University
  • References: <199812180711.CAA03934@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Albert:
The following can easily be developed as a function that handles n
objects taken k at a time.  Here is the code for n objects, taken 2 at
a time:

In[1]:=
n=6;
t=Range[n];
t1=Flatten[Outer[List,t,t],1];
t2=Map[Sort,t1];
t3=Union[t2];
t4=Cases[t3,{x_,y_}/;(x=!=y)]

Out[1]=
{{1,2},{1,3},{1,4},{1,5},{1,6},{2,3},{2,4},{2,5},{2,6},{3,4},{3,5},{3,6},{4,
    5},{4,6},{5,6}}

In[2]:=
Length[t4]
Out[2]=
15

By the way, you have an error in your Diag[] function in your other
posting:

Diag[matrix_List]:=
 
Table[If[i==j,matrix\[LeftDoubleBracket]i,j\[RightDoubleBracket],0],{i,
Length[matrix]},{j,Length[matrix]}]


Cheers,

Des Penny
Physical Science Dept
Southern Utah University
Cedar City, Utah 84720

Voice: (435) 586-7708
FAX: (435) 865-8051
Email: penny at suu.edu


Albert Maydeu-Olivares wrote:

> Hello,
>
> In order to generate all combinations of n objects 2 at a time I load
> the `DiscreteMath` add-on.
>
>  <<DiscreteMath`Combinatorica`
>
> l[n_]:=rt[Ksubsets[Reverse[Table[i,{i,n}]],2]]
>
> So that with n=objects I get
>
> l[6]
>
> {{2,1},{3,1},...,{6,4},{6,5}}
>
> Any suggestions on how to do this without loading the add-on?
>
> --
>
> Albert Maydeu-Olivares                          Tel. +34 3 4021079 ext. 3099 Faculty of
> Psychology                              Fax. +34 3 4021362 University of Barcelona                              E-Mail:
> amaydeu at tinet.fut.es Passeig de la Vall d'Hebron, 171.
> 08035 - Barcelona (Spain)



  • Prev by Date: Re: Question about how Contex resolving works in Mathematica
  • Next by Date: Re: Combinations
  • Previous by thread: combinations of n objects 2 at a time
  • Next by thread: Re: combinations of n objects 2 at a time