Permutation of n arrays

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