两个数组的组合与 Julia 中的排序

Combinations of two arrays with ordering in Julia

如果我有

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

并且我想创建两个列表中长度为 5 的每个组合并进行排序。

到目前为止,我可以通过以下方式获得所有可能的组合:

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

但我只想接收 32 个(在本例中)有序组合。

Mathematica 中的 Tuples[Transpose@{a,b}] 函数与我正在寻找的函数类似。

编辑: Mathematica 输出如下

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

可能最简单的解决方案是简单地过滤掉未排序的元素; filter(issorted, …) 应该可以解决问题。不过,这会产生 26 个元素,所以我可能误解了您的意图:

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

有一个包裹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...)

这是使用 Base.product 的 v0.5 解决方案。

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

创建元组数组

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)

并将结果收集到矩阵中

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