Practical differences between meshgrid and ndgrid?

前端 未结 1 1656
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 08:08

This discussion is getting dense here about pageIdx with ndgrid. I can understand now meshgrid but not ndgrid -- please elaborate with

相关标签:
1条回答
  • 2021-01-03 08:54

    Say you want to know the row/column/etc indices of an array. You can use meshgrid for that, but it will output first the column, then the row indices, which can be a source of error.

    Instead of meshgrid, you can use ndgrid, which will give the indices in the expected order, i.e.

    [rowIndices, colIndices] = ndgrid(1:size(array,1),1:size(array,2));
    

    or, for 3D

    [rowIndices, colIndices, pageIndices] = ndgrid(1:size(array,1),1:size(array,2),1:size(array,3));
    

    meshgrid's inversion of the first two indices can help when dealing with plotting, or with some of the output of Image Processing Toolbox functions.

    However, when you want to calculate, say, the values of a 4D Gaussian, your only choice is to use ndgrid:

    [xx,yy,zz,tt] = ndgrid(-3:0.1:3);
    out = exp(-xx.^2-yy.^2-zz.^2-tt.^2);
    
    0 讨论(0)
提交回复
热议问题