Pretty simple question regarding convhulln. Now that I have K...

13 views (last 30 days)
Hi,
K = convhulln(X) where X is an m-by-n array
According to the documentation, "K = convhulln(X) returns the indices K of the points in X that comprise the facets of the convex hull of X. X is an m-by-n array representing m points in N-dimensional space. If the convex hull has p facets then K is p-by-n." That doesn't really mean a lot to me, because this is the first I've dabbled in geometric algorithms, so I don't even know what a facet is. Right now I just need a quick answer to a pretty simple question, so I can continue with my code.
How do I recover the vertices in X of the complex hull using convhulln (i.e. if I plot the convex hull and then hold and plot the vertices in X, the vertices should line up with those on the convex hull)?

Accepted Answer

John D'Errico
John D'Errico on 12 Jun 2011
(Somebody should post a thing on the FEX explaining all the basics about convex hulls, tessellations, etc. One more thing to add to the round tuit list.)
Think in 2-d first. A convex hull of some data points is a simple polygon, composed of linear segments. Any vertex of the convex hull is also one of the data points. A convex hull does not introduce new points. So essentially a convex hull is just a set of references into the original set of points. Since these things are best done visually, lets generate an example.
>> xy = rand(8,2)
xy =
0.80028 0.84913
0.14189 0.93399
0.42176 0.67874
0.91574 0.75774
0.79221 0.74313
0.95949 0.39223
0.65574 0.65548
0.035712 0.17119
>> plot(xy(:,1),xy(:,2),'o')
See that there appear to be 5 of those points that lie on the boundary of the convex hull in this case, and 3 points lie clearly in the interior.
We can find the hull using either convhull, or convhulln.
>> convhull(xy(:,1),xy(:,2))
ans =
1
2
8
6
4
1
>> convhulln(xy)
ans =
8 6
6 4
4 1
1 2
2 8
What do these numbers mean? Each index returned corresponds to ONE vertex of the hull polygon, but also to one of our original data points. Thus, the convex hull goes between the points [1 2 8 6 4 1] in the set IN THAT ORDER. The points that are inside the hull are of course then numbered [3 5 7].
See that each row of the array returned by convhulln is the same information, just in a slightly different form. It tells us that one edge of the hull is the edge [8 6], connecting point 8 and point 6. Then we connect points 6 and 4, with another edge, etc. These edges in the array returned from convhulln are given in no specific order, but they will be the same information overall as that returned by convhull.
The difference is that convhulln will work in any number of dimensions. (Ok, it can get pretty nasty to visualize in high dimensions. But 3 or 4 or 5 dimensions is entirely adequate. And we can't even draw pictures past 3-d anyway.)
So think about the 2-d case. The convex hull in 2-d is composed of "facets" composed of edges, line segments. In 3-d, the convex hull will always be composed of triangles. A 3-d convex hull will have planar facets. They will always break down into triangular facets though. Yes, one could imagine the set of points that comprise the unit 3-cube. (A cube in R^3.) Clearly its convex hull is a set of squares.
So here are the 8 vertices of the unit 3-cube.
V =
0 0 0
1 0 0
0 1 0
1 1 0
0 0 1
1 0 1
0 1 1
1 1 1
ndgrid could essentially help you to generate that list of points if you wanted.
Let us generate the convex hull using matlab. It is a set of triangles.
>> convhulln(V)
ans =
4 3 1
2 4 1
6 2 1
5 6 1
6 4 2
4 6 8
3 7 1
7 5 1
4 7 3
7 4 8
7 6 5
6 7 8
Each square has been split, into a pair of triangles. Since there are 6 square faces of a cube, there will be 12 triangles.
>> size(ans)
ans =
12 3
If you prefer to visualize them this way, we can sort the triangles in increasing order of the vertices, and then use sortrows. This does not change the information in any serious way.
>> sortrows(sort(convhulln(V),2))
ans =
1 2 4
1 2 6
1 3 4
1 3 7
1 5 6
1 5 7
2 4 6
3 4 7
4 6 8
4 7 8
5 6 7
6 7 8
Still the same information. Each row is ONE triangle. A set of references into the original set of data points. In the case of the unit 3-cube of course, EVERY point lies one the surface, in at least a few of the triangles of that hull. In fact, depending on how those surface squares are split into triangles each point may lie in from 3 to 6 different triangles.
What you need to see is that a convex hull in 3-d is composed of triangles. If we go to higher dimensions, a convex hull in n-dimensions will be composed of (n-1)-simplexes. (A k-simplex is composed of k+1 points. So an (n-1)-simplex is defined by n vertices.) For example...
>> size(convhulln(rand(100,4)))
ans =
290 4
>> size(convhulln(rand(100,5)))
ans =
1068 5
>> size(convhulln(rand(100,6)))
ans =
4047 6
>> size(convhulln(rand(100,7)))
ans =
16094 7
See that each row of the result is a set of indexes into the original array of points, and that in n-dimensions, each row will be a facet, an (n-1) simplex. We can't plot these things on a 2-d monitor or piece of paper, but who cares?
  1 Comment
Aman Singhal
Aman Singhal on 28 Apr 2020
Can you please tell me how can I plot the convex hull with the info. that you have mentioned in the answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Bounding Regions in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!