3
$\begingroup$

I have a question for which I couldn't find an answer: how can we generate only a given number of tuples of a list and not all of them?

For instance, I want to have only the first two 3-tuples of {0, 1}. So the output must look similar to:

{0, 1, 0}, {0, 0, 1}
$\endgroup$
  • 5
    $\begingroup$ It would be useful to describe what ordering you want (note that your "first two" are the third and second elements of Tuples[{0, 1}, 3]) $\endgroup$ – Simon Woods Apr 23 '16 at 15:14
  • 3
    $\begingroup$ Is {0, 1} really the only generator list you are interested in? If that were the case, then one could think of methods based on binary representations of integers, but we still need to have an answer to Simon's question to move any further. $\endgroup$ – MarcoB Apr 23 '16 at 15:29
  • 1
    $\begingroup$ thank you guys for your comments; Actually, what I want is n-tuples of {0,1}, that "n" is a rather high number, like 20 and then I need to take either of these generated tuples as input and do some calculations on. The problem is that my laptop is not powerful enough and i just wanted to generate them in a couple of different steps (instead of all in once). For instance, generating first 100 ones and then generating second 100 ones and so on. $\endgroup$ – Marilla Apr 24 '16 at 9:43
  • $\begingroup$ Is this what you're trying to do? 9554 $\endgroup$ – N.J.Evans Apr 25 '16 at 14:21
5
$\begingroup$

Generate m tuples of length n from elements of the vector v with an optional offset o to skip initial tuples.

mTuples[v_,n_,m_,o_:0]:=Table[v[[IntegerDigits[i,Length[v],n]+1]], {i,o,m-1+o}]

Thus

mTuples[{0,1},3,2] returns {{0,0,0},{0,0,1}}

mTuples[{0,1},3,2,1] returns {{0,0,1},{0,1,0}}

and

mTuples[{a,b,c},2,7,2] returns {{a,c},{b,a},{b,b},{b,c},{c,a},{c,b},{c,c}}

Alternate definition which produces exactly the same result

mTuples[v_,n_,m_,o_:0]:=v[[#]]&/@(IntegerDigits[Range[o,m-1+o],Length[v],n]+1)
$\endgroup$
  • 1
    $\begingroup$ thank you so much for your comment Bill; considering my comment above, i think I can use the way you mentioned. $\endgroup$ – Marilla Apr 24 '16 at 9:44
  • 1
    $\begingroup$ @Marilla If you only need tuples over {0,1} and always want to specify the starting offset then you could simplify my function to mTuples[n_,m_,o_]:=IntegerDigits[Range[o,m-1+o],2,n] $\endgroup$ – Bill Apr 24 '16 at 17:41
2
$\begingroup$

Edited and updated

(There is no requirement for 'Map')

The first two tuples:

IntegerDigits[#, 2, 3] & @ Range[2]

{{0,0,1},{0,1,0}}

 RotateRight@IntegerDigits[#, 2, 3] &@Range[8] == Tuples[{0, 1}, 3]
 RotateRight@IntegerDigits[#, 2, 10] &@Range[1024] == Tuples[{0, 1}, 10]   
 IntegerDigits[#, 2, 10] &@Range[1024][[999]] === Tuples[{0, 1},10][[1000]]

True

True

True

To get all tuples:

IntegerDigits[#, 2, 3] & @ Range[8]

{{0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}, {0, 0, 0}}

To get tuples 5-7, say

IntegerDigits[#, 2, 3] &@Range[5, 7]

{{1,0,1},{1,1,0},{1,1,1}}

To get tuple 4

IntegerDigits[#, 2, 3] &@4

{1, 0, 0}

FromLetterNumber[1 + IntegerDigits[#, 2, 3] & /@ Range[3]]

{{a,a,b},{a,b,a},{a,b,b}}

Finally, in reply to Marilla's comment.

IntegerDigits[#, 2, 10] &@Range[20, 30] // MatrixForm

Matrix of Tuples

IntegerDigits[#, 2, 10] &@21

{0,0,0,0,0,1,0,1,0,1}

$\endgroup$
  • 1
    $\begingroup$ Thanks Tom for your comment; Is there a way also to generate a given number of tuples, not necessarily starting form beginning? For example, instead of first 10 ones, generating second 10 ones. I think Bill's comment considers that, though. $\endgroup$ – Marilla Apr 24 '16 at 9:45
  • $\begingroup$ @Marilla Thanks for your kind comment. To get the tuples 10 to 20, say, where n =10 could be done as follows: IntegerDigits[#, 2, 10] &@Range[10, 20]. I have updated my answer. Note there is no longer a requirement for Map! I also agree with you that Bill's answer is better (and no doubt faster). I did it the 'lazy' way, as I don't like the 'i's and 'j's. $\endgroup$ – user1066 Apr 24 '16 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.