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.
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.
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.
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.
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.
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.
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.
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.
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:
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"]]
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"]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With