My problem is to get the list of all combinations of length 3 where the first place can be 1,2; second place could be a,b; third place could be 1,2,3,4. So the output is should be like this: $\{\{1,a,1\},\{1,a,2\},\cdots,\{2,b,4\}\}$.
Asked
Active
Viewed 358 times
3
1 Answers
7
You want to use Tuples
Tuples[{{1, 2}, {a, b}, {1, 2, 3, 4}}]
Does what you want. Check out the documentation.
Cheers
AndreasP
- 598
- 4
- 10
-
Yes it does the job, thanks! Could you please explain the rule a bit, like if I had length $n$, the first place has $K_1$ possibilities, 2nd place $K_2$ possibilities,...,$n$-th place $K_n$ possibilities? – Marcus Oct 17 '16 at 13:47
-
Tuples. – Marius Ladegård Meyer Oct 17 '16 at 13:42Distributeis another possibility.Distribute[{{1, 2}, {a, b}, {1, 2, 3, 4}}, List] == Tuples[{{1, 2}, {a, b}, {1, 2, 3, 4}}]=> True – user1066 Oct 17 '16 at 19:28