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.