Understanding the difference between ndgrid and meshgrid (from Numpy)

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))

 Accepted Answer

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 Comments

Sorry tto bug you more but if we want, to construct higher dimentional grid, meshgrid does it only up to d = 3, and for d = 4, we have to use ngrid, and that brings us to the permutation problem. How would you overcome this issue?
I would cellfun() a permute() call. Or I would modify the code to support ndgrid ordering. Or I would change the order of arguments to ndgrid()
I am not sure I follow. Let me try to be clear say I have [C = cell(1,3); and C{:}] = meshgrid(x) and it works exactly as I wanted it. But if I try to do C = cell(1,4);[C{:}] = meshgrid(x). I get the error that, that is why I was looking at ndgrid, but that doesn't exactly do the same thing.
Error using meshgrid
Too many output arguments.
Error in meshgrid (line 5184)
[varargout{1:nargout}] = meshgrid(varargin{:});
Error in test (line 39)
meshgrid() is not defined for more than 3 input vectors, so of course ndgrid() is not going to do the same thing.
A = 1 : 4; B = 1 : 3; C = 1 : 2;
[M1, M2, M3] = meshgrid(A,B,C);
has three outputs. All of them are length(B) by length(A) by length(C )
The first one M1 has all of the rows being the values in A
The second of them M2 has all of the columns being the values in B.
The third one M3 has all of the planes being the values in C.
[N1, N2, N3] = ndgrid(A,B,C);
has three outputs. All of them are length(A) by length(B) by length(C )
The first one N1 has all of the columns being the values in A and so
isequal(N1, permute(M1, [2 1 3:ndims(M1)]))
ans = logical
1
The second one N2 has all of the rows being the values in B and so
isequal(N2, permute(M2, [2 1 3:ndims(M2)]))
ans = logical
1
The third one N3 has all of the planes being the values in C and so
isequal(N3, permute(M3, [2 1 3:ndims(M3)]))
ans = logical
1
If you were hypothetically able to extend meshgrid to 4 dimensions, it seems clear to me that the pattern would continue:
A = 1 : 4; B = 1 : 3; C = 1 : 2; D = 1 : 5;
N = cell(1,4);
[N{:}] = ndgrid(A,B,C,D);
M = cellfun(@(m) permute(m, [2 1 3:ndims(m)]), N, 'uniform', 0);
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).

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!