If you are willing to consider another data structure, you can just make a matrix of tuples with:
julia> tuple.(a',b)
2×3 Matrix{Tuple{Int64, Int64}}:
(1, 4) (2, 4) (3, 4)
(1, 5) (2, 5) (3, 5)
or, if you want them in a vector:
julia> vec(tuple.(a',b))
6-element Vector{Tuple{Int64, Int64}}:
(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
(3, 5)
Tuples (and their relatives StaticArrays) are often much faster more convenient to work with than the matlab-style 2-column matrices. Julia has more data structures than just matrices of numbers, and it often pays to exploit them!
In terms of speed, if I define f(a,b) = vec(tuple.(a',b))
, then compared to the combination_vectors
code above it is more than 100x faster. It is about the same speed as the code using reinterpret
by @jacobusmmsmit above, but is simpler and the results will likely be easier to use too (especially if you use StaticArrays instead of tuples).
julia> using BenchmarkTools
julia> f(a,b) = vec(tuple.(a',b));
julia> combination_vectors(a, b) = vcat(([f s] for f in a for s in b)...);
julia> jacobus(a,b) = reshape(reinterpret(Int, collect(Iterators.product(a, b))), (2, :))';
julia> @btime f($a,$b);
19.421 μs (4 allocations: 312.62 KiB)
julia> @btime combination_vectors($a,$b);
3.029 ms (60023 allocations: 4.81 MiB)
julia> @btime jacobus($a,$b);
18.712 μs (2 allocations: 312.55 KiB)