matlab 中 3-D 数组的 1-D 数值梯度

1-D numerical gradient for a 3-D array in matlab

我有一个大小为 (nrows x cols x ndivs) 的 3-D 数组(图像),我希望计算沿第 3 维的一维数值梯度,间距为 0.01,并获得 ( nrows x ncols) 的梯度集。

我想到了一种通过遍历每个像素来做到这一点的方法:

grad_F = zeros(nrows,ncols,ndivs);

for irow = 1:nrows
    for icol = 1:ncols
        grad_F(irow,icol,:) = gradient(F(irow,icol,:),0.01);
    end
end

请问是否有梯度的数组操作可以做到上面的操作而不用遍历每个像素?如果有这样的方法,是更快还是需要相同的时间?

如果我没理解错的话,这应该有用。

[~,~,FZ] = gradient(F,0.01)

FZ 是您在第 3 维中的渐变。