# Generate array of all combinations

**URL:** <https://discourse.julialang.org/t/generate-array-of-all-combinations/30063>\
**Category:** New to Julia\
**Created:** [October 18, 2019, 8:36pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063 "2019-10-18T20:36:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 18, 2019, 8:36pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/1 "2019-10-18T20:36:40Z")

</div>

I want to generate an array of 3 elements

```julia
[α, β, γ]

```

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

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

```

But it failed with error:

```nohighlight
syntax: invalid iteration specification

```

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

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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 18, 2019, 8:49pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/2 "2019-10-18T20:49:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 18, 2019, 9:10pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/3 "2019-10-18T21:10:57Z")

</div>

I think what you want is something like:

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

```

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 18, 2019, 9:15pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/4 "2019-10-18T21:15:13Z")

</div>

> [@aramirezreyes](#):
>
> arr = [[α,β,γ] for α in 0:1 for β in 0:1 for γ in 0:1]

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

```bash
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`,

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 18, 2019, 9:18pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/5 "2019-10-18T21:18:58Z")

</div>

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:

```julia
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:

```julia
Iterators.product(0:0.01:1,0:0.01:1,0:0.01:1)

```

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 18, 2019, 9:21pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/6 "2019-10-18T21:21:23Z")

</div>

Thanks a lot

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 18, 2019, 9:24pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/7 "2019-10-18T21:24:19Z")

</div>

how does your original post exhibit this `0.01` increment feature?

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [October 18, 2019, 9:29pm UTC](https://discourse.julialang.org/t/generate-array-of-all-combinations/30063/8 "2019-10-18T21:29:56Z")

</div>

> [@aramirezreyes](#):
>
> Iterators.product(0:0.001:1,0:0.001:1,0:0.001:1)

I was not aware how to represent it
