Combinations of two arrays with an order in Julia

If i have

a=[1,3,5,7,9]
b=[2,4,6,8,10]

and I want to create each combination of length 5 from two lists with ordering.

So far I can get all possible combinations:

ab=hcat(a,b)
collect(combinations(ab,5))

but I want to get only 32 (in this case) ordered combinations.

A function similar to the one I'm looking for will be the Tuples [Transpose @ {a, b}] function in Mathematica.

EDIT: Mathematica's output will be as follows:

a = {1, 3, 5, 7, 9};
b = {2, 4, 6, 8, 10};
combin = Tuples[Transpose@{a, b}]
Length[combin]

Out[1]:= {{1, 3, 5, 7, 9}, {1, 3, 5, 7, 10}, {1, 3, 5, 8, 9}, {1, 3, 5, 8,
10}, {1, 3, 6, 7, 9}, {1, 3, 6, 7, 10}, {1, 3, 6, 8, 9}, {1, 3, 6,
8, 10}, {1, 4, 5, 7, 9}, {1, 4, 5, 7, 10}, {1, 4, 5, 8, 9}, {1, 4,
5, 8, 10}, {1, 4, 6, 7, 9}, {1, 4, 6, 7, 10}, {1, 4, 6, 8, 9}, {1,
4, 6, 8, 10}, {2, 3, 5, 7, 9}, {2, 3, 5, 7, 10}, {2, 3, 5, 8,
9}, {2, 3, 5, 8, 10}, {2, 3, 6, 7, 9}, {2, 3, 6, 7, 10}, {2, 3, 6,
8, 9}, {2, 3, 6, 8, 10}, {2, 4, 5, 7, 9}, {2, 4, 5, 7, 10}, {2, 4,
5, 8, 9}, {2, 4, 5, 8, 10}, {2, 4, 6, 7, 9}, {2, 4, 6, 7, 10}, {2,
4, 6, 8, 9}, {2, 4, 6, 8, 10}}

Out[2]:= 32
+3
source share
3 answers

Here's a v0.5 solution using Base.product.

WITH

a = [1,3,5,7,9]
b = [2,4,6,8,10]

To create an array of tuples

julia> vec(collect(Base.product(zip(a, b)...)))
32-element Array{Tuple{Int64,Int64,Int64,Int64,Int64},1}:
 (1,3,5,7,9) 
 (2,3,5,7,9) 
 (1,4,5,7,9) 
 (2,4,5,7,9) 
 (1,3,6,7,9) 
 (2,3,6,7,9) 
 (1,4,6,7,9) 
 (2,4,6,7,9) 
 (1,3,5,8,9) 
 (2,3,5,8,9) 
 (2,4,6,7,10)
 (1,3,5,8,10)
 (2,3,5,8,10)
 (1,4,5,8,10)
 (2,4,5,8,10)
 (1,3,6,8,10)
 (2,3,6,8,10)
 (1,4,6,8,10)
 (2,4,6,8,10)

and collect this result into a matrix

julia> hcat((collect(row) for row in ans)...)
5×32 Array{Int64,2}:
 1  2  1  2  1  2  1  2  1  2  1  2  1  …   2   1   2   1   2   1   2   1   2
 3  3  4  4  3  3  4  4  3  3  4  4  3      4   3   3   4   4   3   3   4   4
 5  5  5  5  6  6  6  6  5  5  5  5  6      6   5   5   5   5   6   6   6   6
 7  7  7  7  7  7  7  7  8  8  8  8  8      7   8   8   8   8   8   8   8   8
 9  9  9  9  9  9  9  9  9  9  9  9  9     10  10  10  10  10  10  10  10  10
+5
source

Iterators.jl. ( Pkg.add("Iterators")), :

using Iterators
for p in product([1,2],[3,4],[5,6],[7,8],[9,10])
           @show p
end

:

p = (1,3,5,7,9)
p = (2,3,5,7,9)
p = (1,4,5,7,9)
p = (2,4,5,7,9)
p = (1,3,6,7,9)
p = (2,3,6,7,9)
p = (1,4,6,7,9)
p = (2,4,6,7,9)
p = (1,3,5,8,9)
p = (2,3,5,8,9)
p = (1,4,5,8,9)
p = (2,4,5,8,9)
p = (1,3,6,8,9)
p = (2,3,6,8,9)
p = (1,4,6,8,9)
p = (2,4,6,8,9)
p = (1,3,5,7,10)
p = (2,3,5,7,10)
p = (1,4,5,7,10)
p = (2,4,5,7,10)
p = (1,3,6,7,10)
p = (2,3,6,7,10)
p = (1,4,6,7,10)
p = (2,4,6,7,10)
p = (1,3,5,8,10)
p = (2,3,5,8,10)
p = (1,4,5,8,10)
p = (2,4,5,8,10)
p = (1,3,6,8,10)
p = (2,3,6,8,10)
p = (1,4,6,8,10)
p = (2,4,6,8,10)

, :

arr = Any[]
       for p in product([1,2],[3,4],[5,6],[7,8],[9,10])
                  push!(arr,[y for y in p])
       end
    # now arr is array of arrays. If you want matrix:
    hcat(arr...)
+2

Perhaps the easiest solution is to simply filter out the unsorted elements; filter(issorted, …)gotta do the trick. This gives 26 elements, although perhaps I do not understand your intention:

julia> collect(filter(issorted, combinations(ab,5)))
26-element Array{Array{Int64,1},1}:
 [1,3,5,7,9]
 [1,3,5,7,8]
+1
source

Source: https://habr.com/ru/post/1658739/


All Articles