0
$\begingroup$

I want to use Tuples (if you know better way please let me know) to create the sample space for some events. Assume that I have 2 events each of which can change to a known number (in the case of a coin it is 2). I want to get all of the possible combinations. Something like this:

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

I can use nested for loop but prefer to not.

I used Tuples and Do (see the code below), but it does not give the right answer. What is the problem?

sampleSpaces = 

TableForm[Tuples[{i, j}, 2]]], {i, 0, 1}, {j, 0, 1}];
$\endgroup$
7
  • 2
    $\begingroup$ Table is what you wanted here. TableForm is a formatting wrapper, and should not be used in these cases, only to do visual formatting. $\endgroup$ Jan 21, 2022 at 22:16
  • 1
    $\begingroup$ To add to the comment by @CATrevillian there are extra brackets in the code you provided. Can you try to run sampleSpaces = Table[Tuples[{i, j, k}, 3], {i, 0, 2}, {j, 0, 2}, {k, 0, 2}] and let us know if it works? $\endgroup$
    – user49048
    Jan 21, 2022 at 22:21
  • 1
    $\begingroup$ @Parviz well perhaps you could consider giving a minimal working example (MWE) with the expected outcome so the rest of us can try to help? In the way that the question is written, fixing the code you provided seems the only alternative. $\endgroup$
    – user49048
    Jan 21, 2022 at 22:39
  • 3
    $\begingroup$ Tuples[{0, 1}, 2]? $\endgroup$
    – cvgmt
    Jan 21, 2022 at 23:07
  • 1
    $\begingroup$ RandomChoice[Tuples[{0, 1}, 2], 10]? $\endgroup$
    – cvgmt
    Jan 21, 2022 at 23:08

1 Answer 1

1
$\begingroup$

Using the correct syntax for the command you give we have:

sampleSpaces = Table[Tuples[{i, j}, 2], {i, 0, 1}, {j, 0, 1}];

Then we re-arrange the list in the following manner:

ArrayReshape[sampleSpaces, {16, 2}]

and finally we delete duplicates as

DeleteDuplicates[ArrayReshape[sampleSpaces, {16, 2}]]

which yields

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

and can be generalized, I think, in a straightforward manner.

Edit: you might find it useful to compare sampleSpaces // Dimensions to the number that you see in the ArrayReshape command.

$\endgroup$
5
  • $\begingroup$ Thank you for your help. There is hidden bug in the code. Tuples create all possible combinations include repeating one of the element, in above code if you change the range of i to 0 to 2 it will produce wrong answer. $\endgroup$
    – Parviz
    Jan 22, 2022 at 0:47
  • $\begingroup$ @Parviz you need to make it more clear what it is that you want. At this stage I do not see why you do not just use something like Tuples[{0,2},2]. Please, update your question with this additional essential information. $\endgroup$ Jan 22, 2022 at 1:27
  • 1
    $\begingroup$ As @CATrevillian mentioned Tuples[{0,2},2] is the most minimal and elegant way to approach. I only included the above as an answer, because it was too long for a comment. Also, when I mentioned that you could give a MWE before, I just meant that should explain in the OP with the original numbers you gave to give the expected result, so we can match it in mathematica. And as I mentioned in my comment, I think that Tuples[{0, 1}, 3] gives you what you are after -if I recall the first version of the O.P. $\endgroup$
    – user49048
    Jan 22, 2022 at 1:52
  • $\begingroup$ Thank you for your help. Problem with above solution is that, when I change the range of i {i,0,2}, I will have answer like {2 2 2}, which is not correct (j and k can not get 2 as the value). $\endgroup$
    – Parviz
    Jan 24, 2022 at 18:33
  • $\begingroup$ Solution: Tuples[{Range[0, 3], Range[0, 4], Range[0, 8]}] $\endgroup$
    – Parviz
    Feb 4, 2022 at 19:34

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