Permutation of n arrays

I have n array of arrays and I want to generate a single array containing all permutations of these arrays. For example (with 2 arrays):

a=[[1,2,3],[4,5,6]]
b=[[7,8,9],[10,11,12]]
c=[[[1,2,3],[7,8,9]],[[1,2,3],[10,11,12]],[[4,5,6],[7,8,9]],[[4,5,6],[10,11,12]]]

Can someone suggest an effective way to get c given a and b (in general not just 2 but n arrays)? Thank you.

using Combinatorics

perms = permutations(1:n)

Now index using the contents of each permutation.

What about using Iterators.product, e.g.,

julia> a = [[1,2,3], [4,5,6]] ;

julia> b = [[7,8,9], [10,11,12]] ;

julia> c = [[1, 2], [3, 4], [5,6]] ;

julia> myperms = [collect(x) for x in Iterators.product(a,b,c)]
2×2×3 Array{Array{Array{Int64,1},1},3}:
[:, :, 1] =
 [[1, 2, 3], [7, 8, 9], [1, 2]]  [[1, 2, 3], [10, 11, 12], [1, 2]]
 [[4, 5, 6], [7, 8, 9], [1, 2]]  [[4, 5, 6], [10, 11, 12], [1, 2]]

[:, :, 2] =
 [[1, 2, 3], [7, 8, 9], [3, 4]]  [[1, 2, 3], [10, 11, 12], [3, 4]]
 [[4, 5, 6], [7, 8, 9], [3, 4]]  [[4, 5, 6], [10, 11, 12], [3, 4]]

[:, :, 3] =
 [[1, 2, 3], [7, 8, 9], [5, 6]]  [[1, 2, 3], [10, 11, 12], [5, 6]]
 [[4, 5, 6], [7, 8, 9], [5, 6]]  [[4, 5, 6], [10, 11, 12], [5, 6]]

julia> vec(myperms)
12-element Array{Array{Array{Int64,1},1},1}:
 [[1, 2, 3], [7, 8, 9], [1, 2]]
 [[4, 5, 6], [7, 8, 9], [1, 2]]
 [[1, 2, 3], [10, 11, 12], [1, 2]]
 [[4, 5, 6], [10, 11, 12], [1, 2]]
 [[1, 2, 3], [7, 8, 9], [3, 4]]
 [[4, 5, 6], [7, 8, 9], [3, 4]]
 [[1, 2, 3], [10, 11, 12], [3, 4]]
 [[4, 5, 6], [10, 11, 12], [3, 4]]
 [[1, 2, 3], [7, 8, 9], [5, 6]]
 [[4, 5, 6], [7, 8, 9], [5, 6]]
 [[1, 2, 3], [10, 11, 12], [5, 6]]
 [[4, 5, 6], [10, 11, 12], [5, 6]]

and if your original arrays were themselves stored in another array, you can use splatting

julia> d = [a, b, c] ;

julia> vec([collect(x) for x in Iterators.product(d...)])
12-element Array{Array{Array{Int64,1},1},1}:
 [[1, 2, 3], [7, 8, 9], [1, 2]]
 [[4, 5, 6], [7, 8, 9], [1, 2]]
 [[1, 2, 3], [10, 11, 12], [1, 2]]
 [[4, 5, 6], [10, 11, 12], [1, 2]]
 [[1, 2, 3], [7, 8, 9], [3, 4]]
 [[4, 5, 6], [7, 8, 9], [3, 4]]
 [[1, 2, 3], [10, 11, 12], [3, 4]]
 [[4, 5, 6], [10, 11, 12], [3, 4]]
 [[1, 2, 3], [7, 8, 9], [5, 6]]
 [[4, 5, 6], [7, 8, 9], [5, 6]]
 [[1, 2, 3], [10, 11, 12], [5, 6]]
 [[4, 5, 6], [10, 11, 12], [5, 6]]
1 Like

Thanks. This works perfectly! However, is there a way that we can directly obtain the permutations in the lexicographic order, i.e.

 [[1, 2, 3], [7, 8, 9], [1, 2]]
 [[1, 2, 3], [7, 8, 9], [3, 4]]
 [[1, 2, 3], [7, 8, 9], [5, 6]]
 [[1, 2, 3], [10, 11, 12], [1, 2]]
 [[1, 2, 3], [10, 11, 12], [3, 4]]
 [[1, 2, 3], [10, 11, 12], [5, 6]]
 [[4, 5, 6], [7, 8, 9], [1, 2]]
 [[4, 5, 6], [7, 8, 9], [3, 4]]
 [[4, 5, 6], [7, 8, 9], [5, 6]]
 [[4, 5, 6], [10, 11, 12], [1, 2]]
 [[4, 5, 6], [10, 11, 12], [3, 4]]
 [[4, 5, 6], [10, 11, 12], [5, 6]]

I wasn’t aware of Combinatorics. This seems to be working as intended for simple indexable objects. However, I need to figure it out for my specific case. Thank you.

You can use permutedims:

julia> myperms = permutedims([collect(x) for x in Iterators.product(a,b,c)], (3,2,1))[:]
12-element Array{Array{Array{Int64,1},1},1}:
 [[1, 2, 3], [7, 8, 9], [1, 2]]
 [[1, 2, 3], [7, 8, 9], [3, 4]]
 [[1, 2, 3], [7, 8, 9], [5, 6]]
 [[1, 2, 3], [10, 11, 12], [1, 2]]
 [[1, 2, 3], [10, 11, 12], [3, 4]]
 [[1, 2, 3], [10, 11, 12], [5, 6]]
 [[4, 5, 6], [7, 8, 9], [1, 2]]
 [[4, 5, 6], [7, 8, 9], [3, 4]]
 [[4, 5, 6], [7, 8, 9], [5, 6]]
 [[4, 5, 6], [10, 11, 12], [1, 2]]
 [[4, 5, 6], [10, 11, 12], [3, 4]]
 [[4, 5, 6], [10, 11, 12], [5, 6]]
2 Likes

This works. Thanks a lot!