문제

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]
도움이 되었습니까?

해결책

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.

다른 팁

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top