Generate array of all combinations

I want to generate an array of 3 elements

[α, β, γ]

Where each of the 3 elements α, β, γ is between 0 and 1, so I tried the below

[α for α in (0:1), β for β in (0:1), γ for γ in (0:1)]

But it failed with error:

syntax: invalid iteration specification

I can do something like the below, but do not think it is the correct way:

for α in (0:1), β in (0:1), γ in (0:1)
    println("α: $α, β: $β, γ: $γ")
    push!(w, [α, β, γ])
end
@show w

You can just do [0:1, 0:1, 0:1], if you need the variable names as well consider a NamedTuple

2 Likes

I think what you want is something like:

arr = [[α,β,γ] for α in 0:1 for β in 0:1 for γ in 0:1]
2 Likes

mmm, may be my question statement was not clear, I’ll update it.
your code gave me:

8-element Array{Array{Int64,1},1}:
 [0, 0, 0]
 [0, 0, 1]
 [0, 1, 0]
 [0, 1, 1]
 [1, 0, 0]
 [1, 0, 1]
 [1, 1, 0]
 [1, 1, 1]

What Actual I need, is to include fractions, so it start by 0 any build up to 1 by adding 0.01,

In Julia, a:b is always (I think) using increments of 1 to go from a to b.
You can specify a step c by doing a:c:b

So the example still works if you do:

arr = [[α,β,γ] for α in 0:0.01:1 for β in 0:0.01:1 for γ in 0:0.01:1]

If you don’t need the actual array you can get iterate over the same values with:

Iterators.product(0:0.01:1,0:0.01:1,0:0.01:1)
2 Likes

Thanks a lot

how does your original post exhibit this 0.01 increment feature?

1 Like

I was not aware how to represent it