将矩阵数据表示转换为网格

Converting matrix data representation into meshgrid

我在 Mathematica 中计算了感兴趣的函数 $f(x,y)$。为了保持一致性,我想使用 Matlab 进行绘图。

我以矩阵形式导出了我的函数:

 test =

-1.0000   -1.0000   -0.4864
-1.0000   -0.6000   -0.2804
-1.0000   -0.2000   -0.0462
-1.0000    0.2000   -0.0462
-1.0000    0.6000   -0.2804
-1.0000    1.0000   -0.4864
-0.6000   -1.0000   -0.2997
-0.6000   -0.6000   -0.1526
-0.6000   -0.2000    0.1118
-0.6000    0.2000    0.1118
-0.6000    0.6000   -0.1526
-0.6000    1.0000   -0.2997
-0.2000   -1.0000   -0.1809
-0.2000   -0.6000   -0.0939
-0.2000   -0.2000   -0.0046
-0.2000    0.2000   -0.0046
-0.2000    0.6000   -0.0939
-0.2000    1.0000   -0.1809
 0.2000   -1.0000   -0.1809
 0.2000   -0.6000   -0.0939
 0.2000   -0.2000   -0.0046
 0.2000    0.2000   -0.0046
 0.2000    0.6000   -0.0939
 0.2000    1.0000   -0.1809
 0.6000   -1.0000   -0.2997
 0.6000   -0.6000   -0.1526
 0.6000   -0.2000    0.1118
 0.6000    0.2000    0.1118
 0.6000    0.6000   -0.1526
 0.6000    1.0000   -0.2997
 1.0000   -1.0000   -0.4864
 1.0000   -0.6000   -0.2804
 1.0000   -0.2000   -0.0462
 1.0000    0.2000   -0.0462
 1.0000    0.6000   -0.2804
 1.0000    1.0000   -0.4864

因此 test 是 36x3 矩阵,其中列为 xyf(x,y)。现在我需要绘制等高线图。

不过不是meshgrid的形式。有什么想法,如何快速将其转换为 contour 函数可以绘制的形式?

如果你的数据是规则的(数据集与meshgrid重合但是是矢量形式),那么你可以使用reshape.

xgrid=6;
ygrid=6;
contour(reshape(test(:,1),xgrid,ygrid),reshape(test(:,2),xgrid,ygrid),reshape(test(:,3),xgrid,ygrid))

另一个选项是 tricontour,它也适用于不规则数据集

tri = delaunay(test(:,1),test(:,2));

figure;
tricontour(tri,test(:,1),test(:,2),test(:,3));

您可以在 https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data/content/tricontour.m

找到 tricontour

如果你的表面不是凸面,

那么由于 delaunay

的性质,您需要从 tri 中删除一些三角测量