Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all permutations of the combination of two arrays

Tags:

julia

I am not sure the title is right, below are some explanation:

# I have two arrays: segments and directions
k = 3
segments = collect(1:k)
directions = ["same","reversed"]

# I wish now to generate all possible ways to order the k segments, 
# given that each segment can be either in direction "same" or "reversed"
# so the results should look like:

[[1,"same"], [2,"same"], [3,"same"]]
[[1,"same"], [2,"same"], [3,"reversed"]]
[[1,"same"], [2,"reversed"], [3,"same"]]
[[1,"reversed"], [2,"same"], [3,"same"]]
[[1,"same"], [2,"reversed"], [3,"reversed"]]
...
[[2,"same"], [1,"same"], [3,"same"]]
[[2,"same"], [1,"same"], [3,"reversed"]]
...

# I have tried a few things like this (with no success):
using IterTools
collect(product(segments, directions))

The objective is that each row covers the three segments, each having a single direction.

like image 310
Timothée HENRY Avatar asked Dec 15 '21 16:12

Timothée HENRY


People also ask

How do you generate all permutations of an array?

You take first element of an array (k=0) and exchange it with any element (i) of the array. Then you recursively apply permutation on array starting with second element. This way you get all permutations starting with i-th element.

How can I create every combination possible for the contents of two arrays?

const arr1 = ["A","B","C"]; const arr2 = ["1","2","3"]; We are required to write a JavaScript function that takes in two such arrays of literals. The function should then combine each element of the first array with each element of the second array and push them into a new array.

How do you create an array of all combinations of two Numpy arrays?

Numpy has a function to compute the combination of 2 or more Numpy arrays named as “numpy. meshgrid()“. This function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing.

How do you get all possible number combinations?

In fact, if you know the number of combinations, you can easily calculate the number of permutations: P(n,r) = C(n,r) * r! . If you switch on the advanced mode of this combination calculator, you will be able to find the number of permutations.

How to permutation an array with index 3?

In the permutation (2,3) function, the loop will increase the value of 'i' and will point to the element with index 3 in the array. Also, the variable 'start' is 2 and to continue further, we need to swap these two elements as we were doing above.

How to check if two arrays are permutations of each other?

Given two unsorted arrays of the same size, write a function that returns true if two arrays are permutations of each other, otherwise false. We strongly recommend you to minimize your browser and try this yourself first. A Simple Solution is to sort both arrays and compare sorted arrays.

How do you make permutations of only 3 digits?

And of course, making permutations of only 3 digits is quite easy. So, let's use this logic to make the permutations of the digits 1, 2, 3 and 4. We will start by keeping 1 at the first position. Thus, we are left with the digits 2, 3 and 4. And we have to make all the permutations of the digits 2, 3 and 4.

How can I generate an array with all combinations from two arrays?

First, I created a function that takes 2 arrays and generate an array with all combinations of values from the two arrays: from numpy import * def comb (a,b): c = [] for i in a: for j in b: c.append (r_ [i,j]) return c Then, I used reduce () to apply that to m copies of the same array:


1 Answers

This is an initial answer (which is incorrect, as I incorrectly understood the question, see edit below for a corrected answer).


A natural way to do it is:

julia> collect(Iterators.product(fill(directions, k)...))
2×2×2 Array{Tuple{String, String, String}, 3}:
[:, :, 1] =
 ("same", "same", "same")      ("same", "reversed", "same")
 ("reversed", "same", "same")  ("reversed", "reversed", "same")

[:, :, 2] =
 ("same", "same", "reversed")      ("same", "reversed", "reversed")
 ("reversed", "same", "reversed")  ("reversed", "reversed", "reversed")

since you do not really need the numbers 1,2,3 here as they are encoded by the index. Also using tuples instead of vectors should be a bit more efficient in this case.

If you want to add them and convert the result to a vector of vectors per your question then do:

julia> [vcat.(1:k, x) for x in Iterators.product(fill(directions, k)...)]
2×2×2 Array{Vector{Vector{Any}}, 3}:
[:, :, 1] =
 [[1, "same"], [2, "same"], [3, "same"]]      [[1, "same"], [2, "reversed"], [3, "same"]]
 [[1, "reversed"], [2, "same"], [3, "same"]]  [[1, "reversed"], [2, "reversed"], [3, "same"]]

[:, :, 2] =
 [[1, "same"], [2, "same"], [3, "reversed"]]      [[1, "same"], [2, "reversed"], [3, "reversed"]]
 [[1, "reversed"], [2, "same"], [3, "reversed"]]  [[1, "reversed"], [2, "reversed"], [3, "reversed"]]

finally if you want a vector then do:

julia> [vcat.(1:k, x) for x in Iterators.product(fill(directions, k)...)] |> vec
8-element Vector{Vector{Vector{Any}}}:
 [[1, "same"], [2, "same"], [3, "same"]]
 [[1, "reversed"], [2, "same"], [3, "same"]]
 [[1, "same"], [2, "reversed"], [3, "same"]]
 [[1, "reversed"], [2, "reversed"], [3, "same"]]
 [[1, "same"], [2, "same"], [3, "reversed"]]
 [[1, "reversed"], [2, "same"], [3, "reversed"]]
 [[1, "same"], [2, "reversed"], [3, "reversed"]]
 [[1, "reversed"], [2, "reversed"], [3, "reversed"]]

EDIT

Is this what you want then:

julia> using Combinatorics

julia> x1 = permutations(segments)
Combinatorics.Permutations{Vector{Int64}}([1, 2, 3], 3)

julia> x2 = Iterators.product(fill(directions, k)...)
Base.Iterators.ProductIterator{Tuple{Vector{String}, Vector{String}, Vector{String}}}((["same", "reversed"], ["same", "reversed"], ["same", "reversed"]))

julia> [vcat.(v...) for v in Iterators.product(x1, x2)]
6×2×2×2 Array{Vector{Vector{Any}}, 4}:
[:, :, 1, 1] =
 [[1, "same"], [2, "same"], [3, "same"]]  [[1, "reversed"], [2, "same"], [3, "same"]]
 [[1, "same"], [3, "same"], [2, "same"]]  [[1, "reversed"], [3, "same"], [2, "same"]]
 [[2, "same"], [1, "same"], [3, "same"]]  [[2, "reversed"], [1, "same"], [3, "same"]]
 [[2, "same"], [3, "same"], [1, "same"]]  [[2, "reversed"], [3, "same"], [1, "same"]]
 [[3, "same"], [1, "same"], [2, "same"]]  [[3, "reversed"], [1, "same"], [2, "same"]]
 [[3, "same"], [2, "same"], [1, "same"]]  [[3, "reversed"], [2, "same"], [1, "same"]]

[:, :, 2, 1] =
 [[1, "same"], [2, "reversed"], [3, "same"]]  [[1, "reversed"], [2, "reversed"], [3, "same"]]
 [[1, "same"], [3, "reversed"], [2, "same"]]  [[1, "reversed"], [3, "reversed"], [2, "same"]]
 [[2, "same"], [1, "reversed"], [3, "same"]]  [[2, "reversed"], [1, "reversed"], [3, "same"]]
 [[2, "same"], [3, "reversed"], [1, "same"]]  [[2, "reversed"], [3, "reversed"], [1, "same"]]
 [[3, "same"], [1, "reversed"], [2, "same"]]  [[3, "reversed"], [1, "reversed"], [2, "same"]]
 [[3, "same"], [2, "reversed"], [1, "same"]]  [[3, "reversed"], [2, "reversed"], [1, "same"]]

[:, :, 1, 2] =
 [[1, "same"], [2, "same"], [3, "reversed"]]  [[1, "reversed"], [2, "same"], [3, "reversed"]]
 [[1, "same"], [3, "same"], [2, "reversed"]]  [[1, "reversed"], [3, "same"], [2, "reversed"]]
 [[2, "same"], [1, "same"], [3, "reversed"]]  [[2, "reversed"], [1, "same"], [3, "reversed"]]
 [[2, "same"], [3, "same"], [1, "reversed"]]  [[2, "reversed"], [3, "same"], [1, "reversed"]]
 [[3, "same"], [1, "same"], [2, "reversed"]]  [[3, "reversed"], [1, "same"], [2, "reversed"]]
 [[3, "same"], [2, "same"], [1, "reversed"]]  [[3, "reversed"], [2, "same"], [1, "reversed"]]

[:, :, 2, 2] =
 [[1, "same"], [2, "reversed"], [3, "reversed"]]  [[1, "reversed"], [2, "reversed"], [3, "reversed"]]
 [[1, "same"], [3, "reversed"], [2, "reversed"]]  [[1, "reversed"], [3, "reversed"], [2, "reversed"]]
 [[2, "same"], [1, "reversed"], [3, "reversed"]]  [[2, "reversed"], [1, "reversed"], [3, "reversed"]]
 [[2, "same"], [3, "reversed"], [1, "reversed"]]  [[2, "reversed"], [3, "reversed"], [1, "reversed"]]
 [[3, "same"], [1, "reversed"], [2, "reversed"]]  [[3, "reversed"], [1, "reversed"], [2, "reversed"]]
 [[3, "same"], [2, "reversed"], [1, "reversed"]]  [[3, "reversed"], [2, "reversed"], [1, "reversed"]]
like image 164
Bogumił Kamiński Avatar answered Nov 22 '22 13:11

Bogumił Kamiński