# Meshgrid function in Julia

**URL:** <https://discourse.julialang.org/t/meshgrid-function-in-julia/48679>\
**Category:** Numerics\
**Created:** [October 20, 2020, 12:36pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679 "2020-10-20T12:36:30Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [October 22, 2020, 1:26pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/22 "2020-10-22T13:26:26Z")

</div>

That

> [@stevengj](#):
>
> `@.`

is neat! I would have done .\* but this trick is much better!

---

<div class="post-metadata">

**Author:** ![ajsc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ajsc4/32/20167_2.png) [@ajsc4](https://discourse.julialang.org/u/ajsc4)\
**Post date:** [December 11, 2020, 12:42pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/23 "2020-12-11T12:42:47Z")

</div>

Is the discussion in the link available in plain-text anywhere? I don’t seem to have access to the page.

---

<div class="post-metadata">

**Author:** ![Ionizing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ionizing/32/18070_2.png) [@Ionizing](https://discourse.julialang.org/u/Ionizing)\
**Post date:** [June 18, 2021, 9:22am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/24 "2021-06-18T09:22:23Z")

</div>

I just found an easier way to generate the grid data:

```julia
x = 1:3;
y = 4:6;
z = 7:9;

xv = getindex.(Iterators.product(x, y, z), 1) # first.(Iterators.product(x, y, z), 1) is also ok
yv = getindex.(Iterators.product(x, y, z), 2)
zv = getindex.(Iterators.product(x, y, z), 3) # last.(Iterators.product(x, y, z), 3) is also ok

```

And the result is

```julia
julia> getindex.(Iterators.product(x, y, z), 1)
3×3×3 Array{Int64, 3}:
[:, :, 1] =
 1 1 1
 2 2 2
 3 3 3

[:, :, 2] =
 1 1 1
 2 2 2
 3 3 3

[:, :, 3] =
 1 1 1
 2 2 2
 3 3 3

julia> getindex.(Iterators.product(x, y, z), 2)
3×3×3 Array{Int64, 3}:
[:, :, 1] =
 4 5 6
 4 5 6
 4 5 6

[:, :, 2] =
 4 5 6
 4 5 6
 4 5 6

[:, :, 3] =
 4 5 6
 4 5 6
 4 5 6

julia> getindex.(Iterators.product(x, y, z), 3)
3×3×3 Array{Int64, 3}:
[:, :, 1] =
 7 7 7
 7 7 7
 7 7 7

[:, :, 2] =
 8 8 8
 8 8 8
 8 8 8

[:, :, 3] =
 9 9 9
 9 9 9
 9 9 9

```

which is same as `np.mgrid`’s result. If you want `np.meshgrid` style, the first two dimension should be swapped.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 18, 2021, 12:09pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/25 "2021-06-18T12:09:34Z")

</div>

The easiest way is not to do it at all 😉

---

<div class="post-metadata">

**Author:** ![henry2004y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henry2004y/32/9284_2.png) [@henry2004y](https://discourse.julialang.org/u/henry2004y)\
**Post date:** [June 27, 2021, 8:00am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/26 "2021-06-27T08:00:55Z")

</div>

> [@Ionizing](#):
>
> ```julia
> x = 1:3;
> y = 4:6;
> 
> ```

To get meshgrid style, x and y shall be swapped as

```julia
xv = getindex.(Iterators.product(y, x, z), 2)
yv = getindex.(Iterators.product(y, x, z), 1)

```

However, if you really need this, by far the easiest and fastest I’ve tested is list comprehension:

```julia
function meshgrid(x, y)
   X = [x for _ in y, x in x]
   Y = [y for y in y, _ in x]
   X, Y
end

function meshgrid2(x, y)
   X = getindex.(Iterators.product(y, x), 2)
   Y = getindex.(Iterators.product(y, x), 1)
   X, Y
end

x = 1:1000
y = 1001:2000

```

```julia
julia> @time meshgrid(x,y);
  0.007225 seconds (5 allocations: 15.259 MiB)

julia> @time meshgrid2(x,y);
  0.028644 seconds (9 allocations: 45.777 MiB, 18.41% gc time)

julia> @btime meshgrid($x,$y);
  1.033 ms (4 allocations: 15.26 MiB)

julia> @btime meshgrid2($x,$y);
  4.810 ms (8 allocations: 45.78 MiB)

```

---

<div class="post-metadata">

**Author:** ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)\
**Post date:** [July 12, 2021, 2:32am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/27 "2021-07-12T02:32:52Z")

</div>

There is now a package that implements such a lazy grid for `ngrid` (with lazy `meshgrid` as a one-line variation explained in the docs):

> **[GitHub - JuliaArrays/LazyGrids.jl: A Julia package for representing...](https://github.com/JuliaArrays/LazyGrids.jl)**
>
> A Julia package for representing multi-dimensional grids - GitHub - JuliaArrays/LazyGrids.jl: A Julia package for representing multi-dimensional grids

Hopefully this will help a few more people transition from Matlab to Julia 🙂

---

<div class="post-metadata">

**Author:** ![Arghavan\_Partovifar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arghavan_partovifar/32/18262_2.png) [@Arghavan\_Partovifar](https://discourse.julialang.org/u/Arghavan_Partovifar)\
**Post date:** [August 9, 2021, 2:01pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/28 "2021-08-09T14:01:42Z")

</div>

function meshgrid(n,L) #n= number of grids, L= size of interval  
a=transpose(range(0,L,length=n))  
b=repeat(a,n,1)  
return b  
end

---

<div class="post-metadata">

**Author:** ![IDontReallyCode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/idontreallycode/32/29808_2.png) [@IDontReallyCode](https://discourse.julialang.org/u/IDontReallyCode)\
**Post date:** [October 8, 2021, 9:11pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/29 "2021-10-08T21:11:34Z")

</div>

EDIT: I had a comparison with and without using Meshgrid, but I removed it because there was a mistake in the code and the results were erroneous (on top of using time in the global scope)

The part about LazyGrids being faster seemed to be correct though.

EDIT: using LazyGrids ([https://github.com/JuliaArrays/LazyGrids.jl](https://github.com/JuliaArrays/LazyGrids.jl))  
made it even faster  
0.000021 seconds (15 allocations: 928 bytes)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 8, 2021, 9:21pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/30 "2021-10-08T21:21:10Z")

</div>

Don’t time in the global scope. You’re measuring compilation time.

---

<div class="post-metadata">

**Author:** ![Kaguro](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Kaguro](https://discourse.julialang.org/u/Kaguro)\
**Post date:** [May 1, 2023, 4:20pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/31 "2023-05-01T16:20:29Z")

</div>

change your name from jlchan to jichan

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 1, 2023, 4:31pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/32 "2023-05-01T16:31:18Z")

</div>

You mean Oji-chan?

---

<div class="post-metadata">

**Author:** ![Kaguro](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Kaguro](https://discourse.julialang.org/u/Kaguro)\
**Post date:** [May 1, 2023, 4:32pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/33 "2023-05-01T16:32:33Z")

</div>

😆 yup

---

<div class="post-metadata">

**Author:** ![Kazi\_Abu\_Rousan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kazi_abu_rousan/32/30942_2.png) [@Kazi\_Abu\_Rousan](https://discourse.julialang.org/u/Kazi_Abu_Rousan)\
**Post date:** [November 29, 2023, 5:35pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/34 "2023-11-29T17:35:53Z")

</div>

I was just wondering the same thing and I used something like

```julia
julia> x_i = 0:1:3
0:1:3

julia> y_i = 0:1:3
0:1:3

julia> x_co = [x for x in x_i, y in y_i]
4×4 Matrix{Int64}:
 0 0 0 0
 1 1 1 1
 2 2 2 2
 3 3 3 3

julia> y_co = [y for x in x_i, y in y_i]
4×4 Matrix{Int64}:
 0 1 2 3
 0 1 2 3
 0 1 2 3
 0 1 2 3

```

I guess it’s not needed but still just added if someone needs.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [November 29, 2023, 5:45pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/35 "2023-11-29T17:45:24Z")

</div>

> [@Kazi\_Abu\_Rousan](#):
>
> I guess it’s not needed but still just added if someone needs.

You’re correct that it isn’t needed and actually it isn’t a good idea when there are better alternatives in Julia. See [this](https://discourse.julialang.org/t/how-to-think-about-indices-and-cartesian-coordinates/95972/3) answer its follow-up responses for more detailed discussion.

---

<div class="post-metadata">

**Author:** ![RayhanArch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayhanarch/32/219558_2.png) [@RayhanArch](https://discourse.julialang.org/u/RayhanArch)\
**Post date:** [November 27, 2025, 8:46am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/36 "2025-11-27T08:46:05Z")

</div>

I had the same issue and after watching some youtube videos here’s what I cooked up

```julia
  function Meshgrid(x, y)
      # Reference: https://youtu.be/sHwdoSQpMFQ?si=IKc8e2rmEiTaDZuV
      return repeat(collect(x)', length(y)), repeat(collect(y), 1, length(x))
  end

```

You can use it like numpy,

```julia
using PyPlot

X, Y = Meshgrid(-5:0.5:5, -5:0.5:5);

V = @. -X*Y;
U = 1;
N = @. sqrt(U^2 + V^2);

U = U ./ N;
V = V ./ N;

quiver(X,Y,U,V)

```

 ![test_meshgrid](https://global.discourse-cdn.com/julialang/original/3X/f/f/fffac4f652da06a0f77566d97f8fcdf83a77d386.png)

Hope this helps 😊

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [November 27, 2025, 11:05am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/37 "2025-11-27T11:05:15Z")

</div>

I found Chris’s solution is much faster (2x)

```julia
function meshgrid1(x, y)
    m, n = length(x), length(y)
    Tx, Ty = eltype(x), eltype(y)
    return x' .* ones(Tx, n), ones(Ty, m)' .* y
end

function meshgrid2(x, y)
    # Reference: https://youtu.be/sHwdoSQpMFQ?si=IKc8e2rmEiTaDZuV
    m, n = length(x), length(y)
    return repeat(collect(x)', n), repeat(collect(y), 1, m)
end

```

Benchmark results (Julia 1.12)

```julia
julia> @b (1:10, 1:10) meshgrid1(_[1],_[2])
117.385 ns (8 allocs: 2.125 KiB)
julia> @b (1:10, 1:10) meshgrid2(_[1],_[2])
221.154 ns (9 allocs: 2.172 KiB)

julia> @b (1:10000, 1:10000) meshgrid1(_[1],_[2])
216.570 ms (12 allocs: 1.490 GiB, 13.74% gc time, without a warmup)
julia> @b (1:10000, 1:10000) meshgrid2(_[1],_[2])
404.099 ms (13 allocs: 1.490 GiB, 7.51% gc time, without a warmup)

```

[Previous page](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679.md?page=1)
