I would like to know, if there is any alternate to the np.meshgrid() from NumPy-Python in Julia?
#Python reference : https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html
I would like to know, if there is any alternate to the np.meshgrid() from NumPy-Python in Julia?
#Python reference : https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html
Keep in mind that in Julia you can usually avoid meshgrid using smart broadcast operations. For example. Given a function f(x, y) that operates on scalar values and two "vectors" xs and ys, you can write f.(xs, ys') to produce pretty much any result meshgrid would give you and more:
julia> xs = 0:0.5:1
0.0:0.5:1.0
julia> ys = 0.0:1.0
0.0:1.0:1.0
julia> f(x, y) = (x, y) # equivalent to meshgrid
f (generic function with 1 method)
julia> f.(xs, ys')
3×2 Matrix{Tuple{Float64, Float64}}:
(0.0, 0.0) (0.0, 1.0)
(0.5, 0.0) (0.5, 1.0)
(1.0, 0.0) (1.0, 1.0)
julia> g(x, y) = x*y # more efficient than meshgrid + product
g (generic function with 1 method)
julia> g.(xs, ys')
3×2 Matrix{Float64}:
0.0 0.0
0.0 0.5
0.0 1.0
Following the example in the Python meshgrid manual in Julia you can do:
x = 0.0:0.5:1.0
y = 0.0:1.0
julia> xv = first.(Iterators.product(x,y))
3×2 Matrix{Float64}:
0.0 0.0
0.5 0.5
1.0 1.0
julia> yv = last.(Iterators.product(x,y))
3×2 Matrix{Float64}:
0.0 1.0
0.0 1.0
0.0 1.0
These outcomes are transposed (compared to Python) but in Julia, contrary to Python, the first array index is a row and the second is a column (Julia has column-major arrays)