and I just can not generalizedefining the aforementioned function so that it also generates this other permutations:
tuples[matrix_] := Module[{A, i, h, j, k, m, n},
m = Length[matrix];
n = 1; For[i = 1, i <= m, i++, n = n Length[matrix[[i]]]];
A = ConstantArray[0, {n, m}];
h = 0; k = 1;
For[j = 1, j <= m, j++,
h = h + 1;
For[i = 1, i <= n, i++,
A = ReplacePart[A, {i, j} -> matrix[[h]][[k]]];
If[Mod[i, m / h] == 0, k = k + 1];
If[k > Length[matrix[[j]]], k = 1]
] ]; Return[A]]
and writing:
tuples[{{1, 2, 3, 4}, {5, 6}, {7, 8, 9}}]
I get:
{{1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9},
{3,5, 7}, {3, 5, 8}, {3, 5, 9}, {4, 6, 7}, {4, 6, 8}, {4, 6, 9},
{1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9},
{3, 5,7}, {3, 5, 8}, {3, 5, 9}, {4, 6, 7}, {4, 6, 8}, {4, 6, 9}}
which does not match with repetitionthe output of MMA.
Can someone help me? Thank you!Most likely the problem lies in mod(...)
but I just can not figure out how to correct it.