Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there outer map function in Julia?

Tags:

julia

I am trying to construct all possible combinations of four vectors (parameters in a model) that would give me a big nx4 matrix and I could then run simulation on each set (row) of parameters. In R I would achieve this by using expand.grid in Mathematica style, I could use something like outer product with vcat and reduce the output using hcat.

Is there some function analog of expand.grid from R or outer map function?

Toy example:

A = [1 2]
B = [3 4]

some magic

output = [1 3, 1 4, 2 3, 2 4]
like image 913
krhlk Avatar asked Oct 01 '22 20:10

krhlk


2 Answers

Using the Iterators package, it might look like this:

using Iterators
for p in product([1,2], [3,4])
    println(p)
end

where you would replace println with your algorithm. You can also use collect if it's important to get the set of all combinations.

like image 74
tholy Avatar answered Oct 11 '22 03:10

tholy


Not the exact notation you show, but a comprehension might be useful.

julia> a=[1, 2];

julia> b=[3, 4];

julia> [[i, j] for j in b, i in a]
2x2 Array{Any,2}:
 [1,3]  [2,3]
 [1,4]  [2,4]

julia> [[i, j] for j in b, i in a][:]
4-element Array{Any,1}:
 [1,3]
 [1,4]
 [2,3]
 [2,4]
like image 1
rickhg12hs Avatar answered Oct 11 '22 03:10

rickhg12hs