在 MATLAB 中,您可以使用以下函数griddata https://www.mathworks.com/help/matlab/ref/griddata.html or the TriScatteredInterp class https://www.mathworks.com/help/matlab/ref/triscatteredinterp.html(注:自 R2013a 起scatteredInterpolant https://www.mathworks.com/help/matlab/ref/scatteredinterpolant-object.html是推荐的替代方案)。这两个都允许您将规则间隔的数据表面拟合到一组非均匀间隔的点(尽管看起来griddata
在较新的 MATLAB 版本中不再推荐)。下面介绍了如何使用它们:
-
griddata
:
[XI,YI,ZI] = griddata(x,y,z,XI,YI)
where x,y,z
每个表示每个点(在本例中为等高线上的点)的笛卡尔坐标向量。行向量XI
和列向量YI
是笛卡尔坐标griddata
对值进行插值ZI
的安装表面。为矩阵返回的新值XI,YI
与通过的结果相同XI,YI
to meshgrid https://www.mathworks.com/help/matlab/ref/meshgrid.html创建统一的点网格。
-
TriScatteredInterp
class:
[XI,YI] = meshgrid(...);
F = TriScatteredInterp(x(:),y(:),z(:));
ZI = F(XI,YI);
where x,y,z
再次表示每个点的笛卡尔坐标向量,只是这次我使用了结肠重塑手术 https://www.mathworks.com/help/matlab/ref/colon.html (:)
以确保每个都是列向量(所需的格式为TriScatteredInterp
)。插值法F
然后使用矩阵进行评估XI,YI
您必须使用创建meshgrid
.
示例与比较
下面是一些示例代码及其生成的结果图,用于使用上述两种方法从轮廓数据重建表面。轮廓数据是用contour https://www.mathworks.com/help/matlab/ref/contour.html功能:
% First plot:
subplot(2,2,1);
[X,Y,Z] = peaks; % Create a surface
surf(X,Y,Z);
axis([-3 3 -3 3 -8 9]);
title('Original');
% Second plot:
subplot(2,2,2);
[C,h] = contour(X,Y,Z); % Create the contours
title('Contour map');
% Format the coordinate data for the contours:
Xc = [];
Yc = [];
Zc = [];
index = 1;
while index < size(C,2)
Xc = [Xc C(1,(index+1):(index+C(2,index)))];
Yc = [Yc C(2,(index+1):(index+C(2,index)))];
Zc = [Zc C(1,index).*ones(1,C(2,index))];
index = index+1+C(2,index);
end
% Third plot:
subplot(2,2,3);
[XI,YI] = meshgrid(linspace(-3,3,21)); % Generate a uniform grid
ZI = griddata(Xc,Yc,Zc,XI,YI); % Interpolate surface
surf(XI,YI,ZI);
axis([-3 3 -3 3 -8 9]);
title('GRIDDATA reconstruction');
% Fourth plot:
subplot(2,2,4);
F = TriScatteredInterp(Xc(:),Yc(:),Zc(:)); % Generate interpolant
ZIF = F(XI,YI); % Evaluate interpolant
surf(XI,YI,ZIF);
axis([-3 3 -3 3 -8 9]);
title('TriScatteredInterp reconstruction');
请注意,两个结果之间几乎没有差异(至少在这个范围内)。另请注意,由于这些点处轮廓数据的稀疏性,插值曲面在拐角附近有空白区域。