# Mesh (ndgrid) to points list

**URL:** <https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404>\
**Category:** General Usage\
**Tags:** question, mesh\
**Created:** [May 28, 2025, 11:31am UTC](https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404 "2025-05-28T11:31:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyan](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cyan](https://discourse.julialang.org/u/Cyan)\
**Post date:** [May 28, 2025, 11:31am UTC](https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404/1 "2025-05-28T11:31:41Z")

</div>

Is there any quick method to generate position pairs (x,y) for each points from following mesh grid xg and yg?

```julia
x = range(-2, 2, 50)
y = range(-2, 2, 50)
(xg, yg) = ndgrid(x, y)

```

Finished with `collect(zip(xg,yg))`

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [May 28, 2025, 12:03pm UTC](https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404/2 "2025-05-28T12:03:50Z")

</div>

```julia
x = range(-2, 2, 50)
y = range(-2, 2, 50)

# Scheme 1
xg = zeros(50 * 50)
yg = zeros(50 * 50)
for i = 1:50, j = 1:50
    xg[(i-1)*50+j] = x[i]
    yg[(i-1)*50+j] = y[j]
end

# Scheme 2
g = ((xg, yg) for xg in x, yg in y)
collect(g) # 50×50 Matrix{Tuple{Float64, Float64}}

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [May 28, 2025, 12:34pm UTC](https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404/3 "2025-05-28T12:34:46Z")

</div>

This question has been answered multiple times in the forum.

Here is another solution with broadcasting:

```julia
f(x, y) = (x, y)

f.(xs, ys')

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 28, 2025, 2:01pm UTC](https://discourse.julialang.org/t/mesh-ndgrid-to-points-list/129404/4 "2025-05-28T14:01:51Z")

</div>

There’s also `Iterators.product`, with or without `collect` (usually without, if you can help it), which is essentially the same as the `((xg, yg) for xg in x, yg in y)` solution above but easier to write in an abitrary number of dimensions.

```julia-repl
julia> collect(Iterators.product(x, y))
50×50 Matrix{Tuple{Float64, Float64}}:
 (-2.0, -2.0) (-2.0, -1.91837) … (-2.0, 1.91837) (-2.0, 2.0)
 (-1.91837, -2.0) (-1.91837, -1.91837) (-1.91837, 1.91837) (-1.91837, 2.0)
 (-1.83673, -2.0) (-1.83673, -1.91837) (-1.83673, 1.91837) (-1.83673, 2.0)
 (-1.7551, -2.0) (-1.7551, -1.91837) (-1.7551, 1.91837) (-1.7551, 2.0)
 (-1.67347, -2.0) (-1.67347, -1.91837) (-1.67347, 1.91837) (-1.67347, 2.0)
 (-1.59184, -2.0) (-1.59184, -1.91837) … (-1.59184, 1.91837) (-1.59184, 2.0)
 ⋮ ⋱
 (1.67347, -2.0) (1.67347, -1.91837) … (1.67347, 1.91837) (1.67347, 2.0)
 (1.7551, -2.0) (1.7551, -1.91837) (1.7551, 1.91837) (1.7551, 2.0)
 (1.83673, -2.0) (1.83673, -1.91837) (1.83673, 1.91837) (1.83673, 2.0)
 (1.91837, -2.0) (1.91837, -1.91837) (1.91837, 1.91837) (1.91837, 2.0)
 (2.0, -2.0) (2.0, -1.91837) (2.0, 1.91837) (2.0, 2.0)

```

Although it doesn’t sound like quite what you’re after here, sometimes it can be useful to use `CartesianIndices` for things like this:

```julia
gridaxes = (x,y)
for ci in CartesianIndices(eachindex.(gridaxes))
    point = getindex.(gridaxes, Tuple(ci))
    # do stuff with point
end

```
