This discussion is getting dense here about pageIdx with ndgrid
. I can understand now meshgrid
but not ndgrid
-- please elaborate with
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);