7

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.

3 Answers 3

7

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.

1

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.