Why does gradient function calculates wrong dimensions?

Hi there,
Why does the gradient function calculates the first two dimensions wrong?
Example:
[mshx,mshy,mshz] = ndgrid(0:.1:10,0:.1:10,0:.1:10);
[gxx,gyx,gzx] = gradient(mshx);
[gxy,gyy,gzy] = gradient(mshy);
[gxz,gyz,gzz] = gradient(mshz);
Here all gij are zero except for gyx, gxy, gzz. But how can I calculate the gradient more consistently so that all gij are zero except for gxx, gyy, gzz?
Thank you for your help.
Best regards
Michael

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 30 de Abr. de 2020
Editada: Ameer Hamza el 30 de Abr. de 2020
Use meshgrid instead of ndgrid
[mshx,mshy,mshz] = meshgrid(0:.1:10,0:.1:10,0:.1:10);
[gxx,gyx,gzx] = gradient(mshx);
[gxy,gyy,gzy] = gradient(mshy);
[gxz,gyz,gzz] = gradient(mshz);

3 comentarios

Unfortunately, matlab has a bit of a split personnality when it comes to coordinate systems. For most functions, the first dimension is along the rows, second is along the columns, third along the pages, etc. However, for some functions, (meshgrid, gradient, most plotting functions and most functions of the image processing toolbox), the first dimension is along the columns (x), the 2nd is along the rows (y) and then back to normal dim 3, 4, 5, etc.
This is the problem you hit here, ndgrid follows the 'normal' ordering of dimension whereas gradient follows the swapped 1st and 2nd dimension order.
You can indeed use meshgrid as Ameer answered, or you can reflect the different ordering of ndgrid in the output list:
[mshy, mshx, mshz] = ndgrid(0:0.1:10); %note the swapped x and y
Stephen23
Stephen23 el 30 de Abr. de 2020
Editada: Stephen23 el 30 de Abr. de 2020
Note that using meshgrid is not a general solution, it fails for N==1, i.e. when the input is a vector:
>> mshx = meshgrid(0:10);
>> gradient(mshx) % oops!
ans =
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
It could be made to work by providing the first input argument as a scalar and the second as the vector, but that requires special-case handling. This is not just a theoretical problem: it will be relevant if you write a general solution for any N, e.g. with comma-separated lists as to provide the inputs/outputs of the gridding function and calling gradient in a loop on the outputs.
The solution I gave in my answer to your other (identical) question works for all N, no special cases.
Yes, I accidentally copied my question...
In fact, meshgrid does not work for dimensions greater than 3. That is why there is an inconsistency between ndgrid and gradient.
Thanks for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by