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.
View MATLAB Command Create and plot a polygon made up of four points, and compute its area, perimeter, and centroid coordinates. pgon = polyshape ([0 0 1 3], [0 3 3 0]); plot (pgon) A = area (pgon)
Polygons in two dimensions are generally represented in MATLAB with two arrays, locations for the X vertices and Y vertices. There is no need to have the final points in these match the initial points; that is, when arrays as described are used in situations where they are interpreted as polygon vertices, the polygon is automatically closed.
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.
Each element of the array is 1, which means that each boundary is the exterior boundary of its own region. Display the polygon on a map using the geoshow function, specifying 'DisplayType' as 'polygon'. Clip the polygon to the latitude and longitude limits of Isle Royale National Park using the maptrimp function.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With