Re: Q: extract all k-tuple from a list of n elements
- To: mathgroup at smc.vnet.net
- Subject: [mg49671] Re: Q: extract all k-tuple from a list of n elements
- From: koopman at sfu.ca (Ray Koopman)
- Date: Tue, 27 Jul 2004 07:00:54 -0400 (EDT)
- References: <ce2fv9$8rm$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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}}