【发布时间】:2013-03-24 23:57:54
【问题描述】:
我是新来的,这是我的第一篇文章。我想知道是否有一种方法可以从 100 个随机点计算所有可能的凸包。我创建了一个正确的代码,但是它在 convhull 处给我一个错误,并且无法计算该部分下的所有内容..换句话说,我想知道是否有一种方法可以在 for 循环中使用函数 convhull 而不会出错..
这里是代码
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
这是错误
Error using convhull
Error computing the convex hull. Not
enough unique points specified.
Error in test2 (line 28)
K = convhull(XNV,YNV);
提前致谢
co2ark5
这是在 DAN 的帮助下正确工作的最终代码
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
【问题讨论】:
-
请发布您的代码的一小段相关部分和错误描述,以便我们了解您的错误所在。
-
对于给定的一组点不是只有一个唯一的凸包吗?所有可能的凸包是什么意思?另外请发布代码和错误
-
你是对的,一组点有一个凸包,但每次我从左边的点计算下一个凸包..
标签: matlab convex-hull