Understanding the difference between ndgrid and meshgrid (from Numpy)

20 vues (au cours des 30 derniers jours)
Radoslav Vuchkov
Radoslav Vuchkov le 15 Juin 2021
Modifié(e) : Stephen23 le 16 Juin 2021
Hello everyone,
I am just trying to understand a diffrance between ngrid. Please observe the code below when I check the size of the Ngrid2 I get two cells of $10 \times 10$ and three cells of 15 × 15 × 15, but when I do something similar in numpy meshgrid function I get . Can someone please explain to me the diffrance? Also please advise if there is any way to make Matlab ngrid behave as meshgrid from numpy.
Thank you very much in advance for the consideration!
%Matlab
x = linspace(0,1,5);
[Ngrid2{:}] = ndgrid(x,x);
[Ngrid3{:}] = ndgrid(x,x);
%Python Numpy
x = np.linspace(0,1,5)
z = np.array(np.meshgrid(x,x))
  2 commentaires
Walter Roberson
Walter Roberson le 15 Juin 2021
how did you initialize Ngrid2 and Ngrid3
Radoslav Vuchkov
Radoslav Vuchkov le 15 Juin 2021
My apoligies I used
Ngrid = cell(2,1);

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 15 Juin 2021
Modifié(e) : Stephen23 le 15 Juin 2021
If you must replicate numpy.meshgrid (with the default indexing='xy') then do not use ndgrid, unless you want to waste time permuting all of the output arrays. The correct way to get the same behavior is to use meshgrid:
x = linspace(0,1,5);
C = cell(1,2);
[C{:}] = meshgrid(x);
Checking:
C{:}
ans = 5×5
0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000
ans = 5×5
0 0 0 0 0 0.2500 0.2500 0.2500 0.2500 0.2500 0.5000 0.5000 0.5000 0.5000 0.5000 0.7500 0.7500 0.7500 0.7500 0.7500 1.0000 1.0000 1.0000 1.0000 1.0000
[C{:}] = ndgrid(x); % Note the order of the first two dimensions!
C{:}
ans = 5×5
0 0 0 0 0 0.2500 0.2500 0.2500 0.2500 0.2500 0.5000 0.5000 0.5000 0.5000 0.5000 0.7500 0.7500 0.7500 0.7500 0.7500 1.0000 1.0000 1.0000 1.0000 1.0000
ans = 5×5
0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000
And for comparison (by default the same order as meshgrid):
x = np.linspace(0,1,5)
z = np.array(np.meshgrid(x,x))
print(z[0])
print(z[1])
[[0. 0.25 0.5 0.75 1. ]
[0. 0.25 0.5 0.75 1. ]
[0. 0.25 0.5 0.75 1. ]
[0. 0.25 0.5 0.75 1. ]
[0. 0.25 0.5 0.75 1. ]]
[[0. 0. 0. 0. 0. ]
[0.25 0.25 0.25 0.25 0.25]
[0.5 0.5 0.5 0.5 0.5 ]
[0.75 0.75 0.75 0.75 0.75]
[1. 1. 1. 1. 1. ]]
"Can someone please explain to me the diffrance?"
Look at the order of the first two dimensions in the output arrays.
  • MATLAB's ndgrid corresponds to np.meshgrid(... indexing='ij')
  • MATLAB's meshgrid corresponds to np.meshgrid(... indexing='xy') # default
  6 commentaires
Radoslav Vuchkov
Radoslav Vuchkov le 16 Juin 2021
Thank you so much! This makes sense.
Stephen23
Stephen23 le 16 Juin 2021
Modifié(e) : Stephen23 le 16 Juin 2021
Rather than creating special cases for the first two dimensions you should consider writing your algorithm to use the consistent NDGRID order/orientation instead (just to be clear: this means changing the rest of your code to suit, and not permuting the arrays).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by