# Create iterator for meshgrid of arbitrary dimension

**URL:** <https://discourse.julialang.org/t/create-iterator-for-meshgrid-of-arbitrary-dimension/55820>\
**Category:** New to Julia\
**Created:** [February 23, 2021, 3:35am UTC](https://discourse.julialang.org/t/create-iterator-for-meshgrid-of-arbitrary-dimension/55820 "2021-02-23T03:35:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [February 23, 2021, 3:35am UTC](https://discourse.julialang.org/t/create-iterator-for-meshgrid-of-arbitrary-dimension/55820/1 "2021-02-23T03:35:52Z")

</div>

I am trying to implement a function along the following lines (with the end of evaluating a function over many points in the probability simplex).

```julia
function grid(n, density=5)
    ran = range(0, stop=1, length=density)
    if n==1
        return [i for i in ran]
    elseif n==2
        return [[i, j] for i in ran, j in ran]
    elseif n==3
        return [[i, j, k] for i in ran, j in ran, k in ran]
    # Etc. for arbitrary n
    end
end

```

This works:

```julia
julia> grid(2)
5×5 Array{Array{Float64,1},2}:
 [0.0, 0.0] [0.0, 0.25] [0.0, 0.5] [0.0, 0.75] [0.0, 1.0]
 [0.25, 0.0] [0.25, 0.25] [0.25, 0.5] [0.25, 0.75] [0.25, 1.0]
 [0.5, 0.0] [0.5, 0.25] [0.5, 0.5] [0.5, 0.75] [0.5, 1.0]
 [0.75, 0.0] [0.75, 0.25] [0.75, 0.5] [0.75, 0.75] [0.75, 1.0]
 [1.0, 0.0] [1.0, 0.25] [1.0, 0.5] [1.0, 0.75] [1.0, 1.0]

```

but I understand that it’s not generally a good idea to actually _allocate_ the grid that you are trying to iterate over, so I would like to modify this so that it returns an _iterator_ instead of an array of arrays. And I need it to work for arbitrary `n`.

In context, when the function I want to evaluate is given, I understand that I can replace `[f(i, j) for (i, j) in grid(2)]` with `[f(i,j) for i in ran, j in ran]` where `ran` is as above and `n` is known.

But how do I make this work for arbitrary `n`?

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [February 23, 2021, 3:43am UTC](https://discourse.julialang.org/t/create-iterator-for-meshgrid-of-arbitrary-dimension/55820/2 "2021-02-23T03:43:54Z")

</div>

How about

```julia
Iterators.product(repeat([ran], n)...)

```

Can this be improved upon?

---

<div class="post-metadata">

**Author:** ![Olegg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olegg/32/51316_2.png) [@Olegg](https://discourse.julialang.org/u/Olegg)\
**Post date:** [July 6, 2023, 2:51pm UTC](https://discourse.julialang.org/t/create-iterator-for-meshgrid-of-arbitrary-dimension/55820/3 "2023-07-06T14:51:40Z")

</div>

Except this is not a probability simplex, according to a standard definition. For example, your very last point has sum 1 + 1 = 2.
