i'm new here and this is my first post.. i want to know if there is a way to calculate all the possible convex hulls from 100 random points.. i've created a code that does this right but it gives me an error at convhull and everything beneath that part can't be calculated.. in other words i'd like to know if there is a way i can use function convhull inside a for loop without getting an error..
HERE IS THE CODE
hold on
N=100;
Points = zeros(N,2);
for i=1:N
Points(i,1)= rand(1,1)*100;
Points(i,2)= rand(1,1)*100;
end
SortedX = sortrows(Points,1);
NewVertices = SortedX;
X = reshape(SortedX(:,1),2,[]);
X = X(:);
Y = reshape(SortedX(:,2),2,[]);
Y = Y(:);
plot(X,Y,'*');
while length(NewVertices)>=3
NewVertices = NewVertices(NewVertices~=0);
rowsNV = length(NewVertices(:,1));
NewVertices = reshape(NewVertices,rowsNV/2,2);
XNV = reshape(NewVertices(:,1),rowsNV/2,[]);
XNV = XNV(:);
YNV = reshape(NewVertices(:,2),rowsNV/2,[]);
YNV = YNV(:);
K = convhull(XNV,YNV);
plot(XNV(K),YNV(K),'r-');
for i=1:length(K)-1
NewVertices(K(i),1)=0;
NewVertices(K(i),2)=0;
end
end
HERE IS THE ERROR
Error using convhull
Error computing the convex hull. Not
enough unique points specified.
Error in test2 (line 28)
K = convhull(XNV,YNV);
Thanks in advance
co2ark5
HERE IS THE FINAL CODE THAT WORKS CORRECTLY WITH DAN'S HELP
hold on
N=100;
Points = rand(N,2);
SortedX = sortrows(Points,1);
NewVertices = SortedX;
plot(SortedX(:,1),SortedX(:,2),'*');
while length(NewVertices)>=3
X=NewVertices(:,1);
Y=NewVertices(:,2);
K = convhull(X,Y);
plot(X(K),Y(K),'r-');
for i=1:length(K)-1
NewVertices(K(i),1)=0;
NewVertices(K(i),2)=0;
end
NewVertices = NewVertices(any(NewVertices,2),:);
end
The error is fairly expressive, you aren't specifying enough points (i.e. atleast 3 non colinear points). Please explain what you are trying to achieve with this code.
When do you get this error? I'm assuming not on the first run of the loop? After checking that there are more than 3 points you immediately potentially remove more points and then call convhull so it's quite possible that there are fewer than 3 points (and this is still ignoring the chance of colinearity). After the error, what is size(NewVertices)
?
Some side comments: please explain that whole reshaping business before you plot (maybe add some comments). Also Points
can be initialized much faster and simpler:
Points = rand(N, 2);
EDIT:
From reading your comments below it is clear to me that the logic of your loop is wrong. I cannot work out exactly how you are trying to remove points to create the new subset on which to find the hull, there are no comments and I don't get what you're doing with the reshape
but I'm pretty sure all you need to do to fix this is to move the first line of your loop to the end like this:
while length(NewVertices)>=3
rowsNV = length(NewVertices(:,1));
NewVertices = reshape(NewVertices,rowsNV/2,2);
XNV = reshape(NewVertices(:,1),rowsNV/2,[]);
XNV = XNV(:);
YNV = reshape(NewVertices(:,2),rowsNV/2,[]);
YNV = YNV(:);
K = convhull(XNV,YNV);
plot(XNV(K),YNV(K),'r-');
for i=1:length(K)-1
NewVertices(K(i),1)=0;
NewVertices(K(i),2)=0;
end
NewVertices = NewVertices(NewVertices~=0);
end
This way you find your hull immediately after checking the number of points rather than checking, then removing points, which is why your loop runs on 1 - 2 points.