Matlab:标量场的梯度

Matlab: gradient of a scalar field

我有以下格式的数据。

x y density
. .   .
. .   .
. .   .

这里,density是标量。如何在此数据集上执行梯度?我在 Matlab 中尝试了 gradient 运算符。然而,它returns只是一个标量。

注:xy均以单位间距均匀分布。边界点以浮点数结尾,因为它是裁剪数据。

您可以对数据行进行排序,以便可以将数据点重塑为二维矩阵。然后,您可以计算 that.

的梯度
% Sort so that we get the density into column-major ordering
[~, inds] = sortrows(data(:,[1 2]));

% Reshape the density data so it's [numel(Y) x numel(X)]
density = reshape(data(inds,3), numel(unique(data(:,2))), numel(unique(data(:,1))));

% Compute the X and Y gradients
[FX, FY] = gradient(density);