# Permutation of n arrays

**URL:** <https://discourse.julialang.org/t/permutation-of-n-arrays/54098>\
**Category:** General Usage\
**Tags:** question, first-steps, arrays\
**Created:** [January 28, 2021, 5:13am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098 "2021-01-28T05:13:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [January 28, 2021, 5:13am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/1 "2021-01-28T05:13:35Z")

</div>

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):

```julia
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.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 28, 2021, 5:25am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/3 "2021-01-28T05:25:36Z")

</div>

```julia
using Combinatorics

perms = permutations(1:n)

```

Now index using the contents of each permutation.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [January 28, 2021, 5:48am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/4 "2021-01-28T05:48:00Z")

</div>

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

```julia
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

```nohighlight
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]]

```

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [January 28, 2021, 6:02am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/5 "2021-01-28T06:02:47Z")

</div>

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

```julia
 [[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]]

```

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [January 28, 2021, 6:16am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/6 "2021-01-28T06:16:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [January 28, 2021, 6:23am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/7 "2021-01-28T06:23:48Z")

</div>

You can use `permutedims`:

```julia
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]]

```

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [January 28, 2021, 6:26am UTC](https://discourse.julialang.org/t/permutation-of-n-arrays/54098/8 "2021-01-28T06:26:34Z")

</div>

This works. Thanks a lot!
