用于数据中多个簇的凸壳/凹壳

时间:2014-03-03 17:40:05

标签: matlab polygon cluster-analysis convex-hull

我已经完成了关于在簇周围绘制多边形的大量阅读,并且实现了convhull可能是最好的前进方式。基本上我正在寻找一个弹性的多边形来包裹我的簇点。

我的数据是由x(第1列)和y(第2列)点组成的矩阵,这些点按簇(第3列)分组。我有700个这样的集群,因此不可能分别绘制每个集群。

有没有办法分别为每个群集执行convhull,然后在一张图表上绘制每个群集。

enter image description here

编辑

我写的代码到现在为止,无法在每个集群上运行凸包......

    [ndata, text, alldata] =  xlsread(fullfile(source_dir));

       [~, y] = sort(ndata(:,end));
       As = ndata(y,:);

       lon = As(:,1); 
       lat = As(:,2);
       cluster = As(:,3);

       %% To find number of points in a cluster (repetitions)
   rep = zeros(size(cluster));

   for j = 1:length(cluster)
       rep(j) = sum(cluster==cluster(j));
   end

   %% Less than 3 points in a cluster are filtered out

   x = lon (rep>3);
   y = lat (rep>3);
   z = cluster (rep>3);

   %% convex hull for each cluster plotted ....hold....then display all.

   figure
   hold on

   clusters = unique(z);

       for i = 1:length(z)

          k=convhull(x(z==clusters(i)), y(z==clusters(i)));

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


      end

下面是正在显示的内容的图像;

如果已经提出这个问题,我会为重复道歉,但请指导我看看你认为合适的答案。

任何人都可以帮忙解决这个问题,不管怎么样,我真的很挣扎! enter image description here

1 个答案:

答案 0 :(得分:1)

我将遍历所有聚类并执行您已编写的内容,并使用hold on选项在同一图中累积所有图。像这样:

% Generate three clouds of points in 2D:
c1 = bsxfun(@plus, 0.5 * randn(50,2), [1 3]);
c2 = bsxfun(@plus, 0.6 * randn(20,2), [0 0]);
c3 = bsxfun(@plus, 0.4 * randn(20,2), [1 1]);

data = [c1, ones(50,1); ...
        c2, 2*ones(20,1); ...
        c3, 3*ones(20,1)];

% Plot the data points with different colors

clf
plot(c1(:,1), c1(:,2),'r+', 'LineWidth', 2);
hold on
plot(c2(:,1), c2(:,2),'k+', 'LineWidth', 2);
plot(c3(:,1), c3(:,2),'b+', 'LineWidth', 2);

x = data(:,1);
y = data(:,2);
cluster = data(:,3);

clusters = unique(cluster);

for i = 1:length(clusters)
    px = x(cluster == clusters(i));
    py = y(cluster == clusters(i));
    if length(px) > 2
        k = convhull(px, py);
        plot(px(k), py(k), '-');
    end
end

它给出了以下结果: 2D Clusters and their corresponding Convex Hull