MathGroup Archive 2004

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

Search the Archive

Re: Q: extract all k-tuple from a list of n elements


Klaus Duellmann <klaus.duellmann at arcor.de> wrote in message 
news:<ce2fv9$8rm$1 at smc.vnet.net>...
> Question: How can I extract all k-tuple from a list of n elements 
> (without considering permutations of the k-tuple)?
> 
> Example: For the special case k=3 one solution would be
> 
> Flatten[Table[{i, j, k}, {i, 1, n - 2}, {j, i + 1, n - 1}, {k, j + 1, 
> n}], 2];
> 
> A generalization of this solution for all k >=1 would involve to create 
> 'automatically' a table of dimension k, but how can this be implemented?
  
In[1]:= subsets[n_,k_] := ToExpression["Flatten[Table[" <> 
         ToString[Table["i"<>ToString[j],{j,k}]] <> 
         Table[ ", {i"<>ToString[j] <> 
          If[j==1, ",", ",i"<>ToString[j-1]<>"+1,"] <> 
          ToString[n+j-k] <> "}", {j,k}] <> 
         "]," <> ToString[k-1] <> "]"]
  
In[2]:= subsets[5,3]
Out[2]= {{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5},
         {1, 4, 5}, {2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5}}


  • Prev by Date: RE: Q: extract all k-tuple from a list of n elements
  • Next by Date: Re: Q: extract all k-tuple from a list of n elements
  • Previous by thread: RE: Q: extract all k-tuple from a list of n elements
  • Next by thread: Re: Q: extract all k-tuple from a list of n elements