Reputation: 610
If I have
a=[1,3,5,7,9]
b=[2,4,6,8,10]
and I want to create every combination of length 5 of the two lists with ordering.
So far I can get every possible combination through:
ab=hcat(a,b)
collect(combinations(ab,5))
but I want to receive only the 32 (in this case) ordered combinations.
A function similar to what I am looking for would be the Tuples[Transpose@{a,b}] function in Mathematica.
EDIT: Mathematica output would 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
Upvotes: 5
Views: 2924
Reputation: 1824
There is a package Iterators.jl. By using it (First you should install it by Pkg.add("Iterators")
) you can do the following:
using Iterators
for p in product([1,2],[3,4],[5,6],[7,8],[9,10])
@show p
end
Output:
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)
EDIT
To get the results as array of arrays or matrix you can do :
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...)
Upvotes: 4
Reputation: 12051
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 to collect that 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
Upvotes: 10
Reputation: 31362
Probably the simplest solution is to simply filter out the unsorted elements; filter(issorted, …)
should do the trick. This yields 26 elements, though, so perhaps I'm misunderstanding 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]
⋮
Upvotes: 1