MathGroup Archive 2005

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

Search the Archive

Re: Re: Combinations


Sseziwa Mukasa wrote:
> On Oct 26, 2005, at 1:01 AM, xxxxyz at abv.bg wrote:
> 
> 
>>Hi,
>>
>>Is there any way to generate combinations per turn in a cycle? I mean
>>first generate one combination, do something, generate next, do the
>>same thing ...
>>
>>Thanks.
> 
> 
> The combinations are called KSubsets in Mathematica, the  
> Combinatorica package contains the necessary routines.  The first  
> KSubset in lexicographic order is simply Range[k], you can use  
> NextKSubset to see the others for example:
> 
> <<DiscreteMath`Combinatorica`
> Block[{subset = Range[3]},Prepend[Table[subset = NextKSubset[Range 
> [5], subset], {4}], Range[3]]]
> 
> will return the first 5 subsets of length 3 of {1,2,3,4,5} in  
> lexicographic order.  There are functions to get the KSubsets in  
> other orders in Combinatorica, I suggest you read the Help Browser  
> information or read "Computational Discrete Mathematics:  
> Combinatorics and Graph Theory with Mathematica" by Steven Skiena and  
> Sriram Pemmaraju for more information on the package.
> 
> Regards,
> 
> Ssezi
> 

Starting in version 5.1, the function Subsets will also do the above:

In[3]:=
Subsets[{1,2,3,4,5},{3},5]

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

If we are interested in the 9th subset of {1,2,3,4,5} of length 3:

In[4]:=
Subsets[{1,2,3,4,5},{3},{9}]

Out[4]=
{{2,4,5}}

Carl Woll
Wolfram Research


  • Prev by Date: New LinkageDesigner application for mechanical design with Mathematica
  • Next by Date: Re: A Problem with the NonlinearFit?
  • Previous by thread: Re: Combinations
  • Next by thread: multidimensional interpolation