Question

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]
Was it helpful?

Solution

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.

OTHER TIPS

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top