Contour not taking into account edge of domain

3 ビュー (過去 30 日間)
lepolonais03
lepolonais03 2025 年 1 月 22 日
編集済み: Paul 2025 年 1 月 24 日
Hello eveyone,
I have an issue using contour (contourf or contourc as well) with convhull in order to get the convex polygon that includes the domain where Z=1.
When i try to retrieve the contour matrix, only the coordinates contained are those of strictly inside the domain, and the edges of the domain do not appear, then the polygon I get is way smaller than I expect.
I hope this is rather clear. Thank you in advance!
Here is my code
X = 1:0.2:2;
Y = 2:0.2:3;
[X_mesh,Y_mesh]=meshgrid(X,Y);
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
figure,
[M,h]=contourf(X_mesh,Y_mesh,Z,[1 1]);
M
M = 2×9
1.0000 1.4000 1.6000 1.6000 1.6000 1.4000 1.6000 1.8000 2.0000 8.0000 2.0000 2.2000 2.2000 2.2000 2.4000 2.6000 2.6000 2.6000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
coords_x=M(1,2:end);
coords_y=M(2,2:end);
K=convhull(coords_x,coords_y);
figure,hold on,
contourf(X_mesh,Y_mesh,Z,[1 1]);
patch(coords_x(K),coords_y(K),'green', 'FaceAlpha', 0.5);

採用された回答

lepolonais03
lepolonais03 2025 年 1 月 24 日
I found a workaround. I added zeros all around the contour I wanted in order to virtually 'erase' the edges.
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
Z_extended = [zeros(1,size(Z,2)+2);
[zeros(size(Z,1),1),Z,zeros(size(Z,1),1)];
zeros(1,size(Z,2)+2)]
  1 件のコメント
Paul
Paul 2025 年 1 月 24 日
編集済み: Paul 2025 年 1 月 24 日
Doesn't this approach yiled the same polygon as in the original code?
Z = [1 1 1 0 0 0;
1 1 1 1 0 0;
1 1 1 0 0 0;
1 1 1 1 1 1;
1 1 1 1 1 1;
1 1 1 1 1 1];
Z_extended = [zeros(1,size(Z,2)+2);
[zeros(size(Z,1),1),Z,zeros(size(Z,1),1)];
zeros(1,size(Z,2)+2)];
X = [0.8 , 1:0.2:2 , 2.2];
Y = [1.8 , 2:0.2:3 , 3.2];
[X_mesh,Y_mesh]=meshgrid(X,Y);
figure,
[M,h]=contourf(X_mesh,Y_mesh,Z_extended,[1 1]);
I confess that I don't understand why M has so many points, and some vertices are repeated.
M.'
ans = 28×2
1.0000 27.0000 1.0000 2.0000 1.0000 2.0000 1.2000 2.0000 1.4000 2.0000 1.4000 2.0000 1.6000 2.2000 1.6000 2.2000 1.6000 2.2000 1.4000 2.4000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by