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

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:

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

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

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]
1 Like

Eg something like

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

would give you

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]
1 Like

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

1 Like

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.