Using ndgrid / meshgrid functions in Julia

I am trying to find functionality in Julia similar to MATLAB meshgrid

or ndgrid

. I know what Julia defined ndgrid

in the examples , but when I try to use her I get the following error.

UndefVarError: ndgrid is not defined

Does anyone know how to get the inline function ndgrid

to work, or perhaps another function I haven't found, or a library that provides these methods (inline function preferred)? I would rather not write in this case.

Thank!

+3


source to share


1 answer


We prefer to avoid these functions as they allocate arrays that are not normally needed. The values ​​in these arrays are structured so regularly that they do not need to be stored; they can be simply computed during iteration. For example, one alternative approach is to write an array comprehension:

julia> [ 10i + j for i=1:5, j=1:5 ]
5Γ—5 Array{Int64,2}:
 11  12  13  14  15
 21  22  23  24  25
 31  32  33  34  35
 41  42  43  44  45
 51  52  53  54  55

      



Or you can write loops for

or iterate over an iterator product

:

julia> collect(Iterators.product(1:2, 3:4))
2Γ—2 Array{Tuple{Int64,Int64},2}:
 (1, 3)  (1, 4)
 (2, 3)  (2, 4)

      

+5


source







All Articles