# How to generate the permutation of a vector with elements selected from a certain set

**URL:** <https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032>\
**Category:** General Usage\
**Created:** [March 16, 2020, 6:50am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032 "2020-03-16T06:50:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)\
**Post date:** [March 16, 2020, 6:50am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032/1 "2020-03-16T06:50:57Z")

</div>

Hi guys I am looking for some function that generates the permutation of a vector of a certain length with elements taken from a certain set. For example let us say we are generating the permutation of vectors of length 3 with elements taken from set (0, 1) and we have:

```julia
[0 0 0], [0 0 1], [0 1 0], [1 0 0], [1 0 1], [1 1 0], [0 1 1], [1 1 1]

```

also I would like to get even the number of such permutation in this case:

```julia
noOfPerm = length(set)^length(vector) = 2^3 = 8

```

thank you very much in advance for your time and support.

Cheers.

Ergnoor

p.s. I already had a look at Combinatorics.jl

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 16, 2020, 8:16am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032/2 "2020-03-16T08:16:54Z")

</div>

```julia
julia> [[x,y,z] for x=0:1 for y=0:1 for z=0:1]
8-element Array{Array{Int64,1},1}:
 [0, 0, 0]
 [0, 0, 1]
 [0, 1, 0]
 [0, 1, 1]
 [1, 0, 0]
 [1, 0, 1]
 [1, 1, 0]
 [1, 1, 1]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 16, 2020, 8:51am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032/3 "2020-03-16T08:51:16Z")

</div>

Eg something like

```julia
all_perm(xs, n) = vec(map(collect, Iterators.product(ntuple(_ -> xs, n)...)))

```

would give you

```julia
julia> all_perm([0, 1], 3)
8-element Array{Array{Int64,1},1}:
 [0, 0, 0]
 [1, 0, 0]
 [0, 1, 0]
 [1, 1, 0]
 [0, 0, 1]
 [1, 0, 1]
 [0, 1, 1]
 [1, 1, 1]

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [March 16, 2020, 10:19am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032/4 "2020-03-16T10:19:14Z")

</div>

If you want the “uncollected” form to only extract the length without the allocaiton, you can do:

`all_perm(x, n) = Iterators.product([x for i = 1:n]...)`

You can still do `length(all_perm([0, 1], 3))` on this but you didn’t allocate all the permutations (in case there are many). But you would have to use `vec(collect(all_perm([0,1], 3))` to get the result as a vector rather than a matrix (the default for `ProductIterator`).

---

<div class="post-metadata">

**Author:** ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)\
**Post date:** [March 23, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-to-generate-the-permutation-of-a-vector-with-elements-selected-from-a-certain-set/36032/5 "2020-03-23T00:01:18Z")

</div>

Hi guys and thank you so much for your answers and the the help provided. I have not been in laboratory for days because of covid 19 and that’s the reason for this late answer and I am sorry for this. I will let you know later after the “restriction” to access laboratory will be lifted for my decision which one will be the solution for me. I appreciate very much the help from all of you thou. Cheers Ergnoor.
