Search code examples
matlabconvex-hull

Matlab Convhull Error. Not enough unique data point


I have been receiving this error in Matlab when I tried to calculate the convex hull of a set of numbers inscribed by a circle. Here is the code, and the error I kept getting is: Error computing the convex hull. Not enough unique points specified.

for u = 1:50; 
  for v = 1:50;       
    if sqrt(((u-25)^2)+((v-25)^2)) <= 25
      c = convhull(u,v);
      plot(u(c),v(c),'r-',u,v,'b*')         
    end
  end
end

Points inscribed or on a circle: enter image description here


Solution

  • In your code you are sending individual points to convhull. Instead determine all the points in the set first, then send them all to the function at once. Here's an example.

    % create mesh
    [u,v] = meshgrid(1:50,1:50);
    % get indicies of points within the circle
    idx = sqrt((u-25).^2+(v-25).^2) <= 25;
    % filter outside points
    u = u(idx);
    v = v(idx);
    % compute convex hull
    c = convhull(u,v);
    plot(u(c),v(c),'r-',u,v,'b.');
    

    Results

    enter image description here


    Side note: Formally a singleton set is convex thus it's its own convex hull. I'm not sure why MathWorks decided to return an error in this case.