在 MATLAB 中围绕数据点组绘制多边形

发布于 2024-08-31 19:34:56 字数 101 浏览 0 评论 0原文

我有一组数据点,每个数据点都属于某个集群(组)。我需要围绕每个簇绘制一个多边形。有谁知道该怎么做?

我是否使用实际数据点来绘制多边形并不重要。我只需要将它们包裹在多边形中。

I have a set of datapoints each of which belongs to a certain cluster (group). I need to draw a polygon around each of these clusters. Does anyone knows how to do it?

It doesn't matter if I use or not use the actual datapoints for drawing the polygon. I just need them to be wrapped in a polygon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-09-07 19:34:56

尝试使用 convhull 函数。它返回数据集中定义凸包的点的索引。您必须对绘制的每个簇执行此操作。

例如:

x=rand(1,100); %#generate x and y data for your clusters
y=rand(1,100);
k=convhull(x,y); %#generate indices marking the outermost points

hold on
plot(x,y,'b.') %# plot your cluster points
plot(x(k),y(k),'r-') %# plots only k indices, giving the convex hull

这将为您提供一个多边形,其索引与集群的异常值一致。

Try the convhull function. It returns the indices from the points in your data set that will define the convex hull. You'll have to do this for each cluster that you plot.

For example:

x=rand(1,100); %#generate x and y data for your clusters
y=rand(1,100);
k=convhull(x,y); %#generate indices marking the outermost points

hold on
plot(x,y,'b.') %# plot your cluster points
plot(x(k),y(k),'r-') %# plots only k indices, giving the convex hull

This will give you a polygon whose indices coincide with the outliers of your clusters.

江城子 2024-09-07 19:34:56

我不确定是否有预制的解决方案,因为我对 MATLAB 不太熟悉,但这听起来像您需要一个凸包解决方案。

希望这能为您指明正确的方向。

I'm not sure if there's a pre-made solution for this as I'm not too familiar with MATLAB, however this sounds like you need a convex hull solution.

Hope this points you in the right direction.

默嘫て 2024-09-07 19:34:56

仅当您具有凸形状(如椭球体)时,Convhull 才有效。如果您的数据分布具有凹曲线,例如香蕉形状,则 convhull 将不起作用。幸运的是,MATLAB 有一个函数可以处理这个问题:alphashape

根据“alpha”值,您可以在生成的多边形中获得更多或更少的面。

一旦获得了面的 x,y 坐标,您可以直接绘制它们,但多边形将具有平坦的边,或者:

您可以定义一个 x,y,z 网格来在其中查看数据,而不是插值,并且问,x,y 是否在 alpha 形状内?如果是,则给它一个值 z = 1,如果不给它一个值 z = 0。然后简单地在 z = 1 处绘制网格轮廓。

手动绘制多边形

您还可以使用 impoly在 Matlab 文件交换上 hobbysplines 也允许平滑多边形的边缘

convhull only works if you have a convex shape (like an ellipsoid). If your data distribution has concave curves, such as a banana shape, then convhull will not work. Luckily, MATLAB has a function to handle this: alphashape

depending on the "alpha" value, you get more or less facets in the resulting polygon.

once you have the x,y coordinates of the facets, you can either plot them directly but the polygon will have flat sides, or:

instead of interpolating, you can define an x,y,z grid within which to view the data, and ask, is x,y within the alpha shape? If it is, give it a value z = 1 and if not give it a value z = 0. then simply contour the grid where z = 1.

you can also use impoly to draw the polygon manually

hobbysplines on the Matlab file exchange also allows you to smooth the edges of a polygon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文