Question

I have done a lot of reading on drawing polygons around clusters and realized convhull maybe the best way forward. Basically I am looking for a elastic like polygon to wrap around my cluster points.

My data is matrix consisting of x (1st column) and y(2nd column) points which are grouped in clusters (3rd column). I have 700 such clusters hence not feasible to plot each separately.

Is there a way to perform convhull for each cluster separately and then plot each of them on a single chart.

enter image description here

EDIT

Code I have written until now which isn't able to run convex hull on each individual cluster...

    [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

Below is an image of what is being displayed;

If this question has already been asked I apologize for repetition but please do direct me to the answer you'll see fit.

Please can anyone help with this, however trivial I'm really struggling! enter image description here

Was it helpful?

Solution

I would iterate through all the clusters and do what you already written, and use the hold on option to accumulate all the plots in the same plot. Something like this:

% 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

It gives the following result: 2D Clusters and their corresponding Convex Hull

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top