# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [October 20, 2020, 12:36pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/1 "2020-10-20T12:36:30Z")

</div>

hello everyone I am new to Julia. Earlier I was using Matlab but now I have started to work in Julia. I would like to know is there any similar function available in julia like Meshgrid which is available in matlab. Please help.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [October 20, 2020, 12:58pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/2 "2020-10-20T12:58:17Z")

</div>

Yup, see VectorizedRoutines.jl

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 20, 2020, 2:56pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/3 "2020-10-20T14:56:40Z")

</div>

Also worth saying: as you use Julia more and more, you’ll probably use `meshgrid` less and less. Here’s a nice explanation of why (for an outdated Julia version, but other than suggesting you ignore the comments about performance—all those issues were fixed long ago—the syntax seems pretty applicable now): [https://groups.google.com/g/julia-users/c/83Pfg9HGhGQ/m/9G\_0wi-GBQAJ](https://groups.google.com/g/julia-users/c/83Pfg9HGhGQ/m/9G_0wi-GBQAJ)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [October 20, 2020, 4:25pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/4 "2020-10-20T16:25:44Z")

</div>

A few months ago, while teaching conformal mapping, I rolled my own

```julia
"""
duplicate of matlab meshgrid function
"""
function meshgrid(xin,yin)
nx=length(xin)
ny=length(yin)
xout=zeros(ny,nx)
yout=zeros(ny,nx)
for jx=1:nx
    for ix=1:ny
        xout[ix,jx]=xin[jx]
        yout[ix,jx]=yin[ix]
    end
end
return (x=xout, y=yout)
end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 20, 2020, 4:31pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/5 "2020-10-20T16:31:11Z")

</div>

Modern Julia is better:

```julia
x = 1:3
y = 1:5
x' .* ones(5)

#=
5×3 Array{Float64,2}:
 1.0 2.0 3.0
 1.0 2.0 3.0
 1.0 2.0 3.0
 1.0 2.0 3.0
 1.0 2.0 3.0
=#
 
ones(3)' .* y

#=
5×3 Array{Float64,2}:
 1.0 1.0 1.0
 2.0 2.0 2.0
 3.0 3.0 3.0
 4.0 4.0 4.0
 5.0 5.0 5.0
=#

```

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [October 20, 2020, 4:32pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/6 "2020-10-20T16:32:37Z")

</div>

neat! when was that?

also, can you do 3D arrays with the same bcast trick?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 20, 2020, 4:32pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/7 "2020-10-20T16:32:37Z")

</div>

The only reason to make a meshgrid in Julia now is that you can do it as a lazy object without an array backing, so it could be like a 2D or 3D `Range`, meaning a 10,000,000 x 10,000,000 grid is representable in like 500 bytes.

---

<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:** [October 20, 2020, 4:33pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/8 "2020-10-20T16:33:44Z")

</div>

You don’t need meshgrid in Julia. In fact, it is rarely needed even in Matlab these days, after they introduced a type of broadcasting.

I would recommend to not use meshgrid, and instead learn idiomatic Julia.

---

<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:** [October 20, 2020, 4:39pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/10 "2020-10-20T16:39:05Z")

</div>

Same thing with `repmat`. Never needed in Julia, _virtually_ never needed in Matlab. It’s old-style Matlab.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [October 20, 2020, 4:42pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/11 "2020-10-20T16:42:31Z")

</div>

> old-style Matlab.

This was the moment I realized I am now old.

---

<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:** [October 20, 2020, 4:43pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/12 "2020-10-20T16:43:58Z")

</div>

I meant “classic Matlab” 😉

---

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [October 20, 2020, 6:23pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/13 "2020-10-20T18:23:18Z")

</div>

Can you help with how to plot a structured grid using these points in Julia?

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [October 20, 2020, 7:49pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/14 "2020-10-20T19:49:33Z")

</div>

I am also confused. I use this for `contour` in PyPlot, where the help files tell me to use meshgrid. How can I avoid meshgrid without finding a different plotting package?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 20, 2020, 8:02pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/15 "2020-10-20T20:02:23Z")

</div>

> [@ctkelley](#):
>
> I am also confused. I use this for `contour` in PyPlot, where the help files tell me to use meshgrid. How can I avoid meshgrid without finding a different plotting package?

You don’t need a meshgrid for contour plotting with PyPlot — you can pass 1d arrays for the axes and it knows how to broadcast them, and you write 2d functions of the axes variables by broadcast operations as well:

```julia
using PyPlot
x = range(0,2,length=100)' # note ': this is a row vector
y = range(0,4,length=200)
z = @. sin(x) * cos(y) # broadcasts to 2d array
contour(x, y, z)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/a/8a94c760c53802c3cca3a28417f1c621855431e5.png)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [October 20, 2020, 8:15pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/16 "2020-10-20T20:15:53Z")

</div>

Thanks. I’m even older than @jlchan and transcribed my stuff from (no joke) 20 year old matlab.

---

<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:** [October 20, 2020, 9:04pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/17 "2020-10-20T21:04:27Z")

</div>

This is the corresponding Matlab code, btw:

```julia
x = linspace(0, 2, 100);
y = linspace(0, 4, 200).';
z = sin(x) .* cos(y); % broadcasts to 2d array
contour(x, y, z)

```

 ![figure1](https://global.discourse-cdn.com/julialang/original/3X/4/3/4370bf7a3b47eed07a1086e00ff99a9544412852.png)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [October 20, 2020, 9:20pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/18 "2020-10-20T21:20:06Z")

</div>

I have seen the error in my ways and will go forth and sin in a different way in the future.

---

<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:** [October 20, 2020, 9:42pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/19 "2020-10-20T21:42:42Z")

</div>

> [@ctkelley](#):
>
> sin in a different way in the future.

Remember to cos as well!

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [October 20, 2020, 10:15pm UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/20 "2020-10-20T22:15:54Z")

</div>

I will do that as I sit in the sun and work on my tan.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 21, 2020, 10:25am UTC](https://discourse.julialang.org/t/meshgrid-function-in-julia/48679/21 "2020-10-21T10:25:08Z")

</div>

Instead of `sin`ning, `sec` and ye shall find!

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