Different answers for the same algorithm

조회 수: 2 (최근 30일)
farah fadel
farah fadel 2020년 3월 20일
댓글: farah fadel 2020년 3월 28일
I am getting different values of the width upon calling the findwidth function or writing its algorithm (exactly the same) in the main script. Is there a reason to justify that and know what is true?
The main script looks like this:
for i=1:10
:
for j=1:13
for p=1:length(c1)
:
P1new=[P11(concavev(i):n,:);P11(1:c1(p),:)];
width(i,j,p)=findwidth(P1new)
end
end
end
%the width function :
function width=findwidth(P11)
[k,av] = convhull(P11);
CP=[P11(k,1),P11(k,2)];
for x=1:(length(CP)-1)
z=1:(length(CP)-1);
for z=1:(length(CP)-1);
D(x,z)=Seg_point_dis(CP(z,:),CP(z+1,:),CP(x,:))
end
end
D1=max(D,[],2)
width=min(D1);
end
  댓글 수: 16
Guillaume
Guillaume 2020년 3월 27일
I've only checked the first few lines of your code and stopped at CheckConc.
You're still using length on a 2-column matrix (both in the script and in CheckConc. I recommend that you never use length. For a vector, use numel. For a matrix, use size and explicitly specify which dimension you want to know the size of, rather than assuming that length will return the size of the dimension you are hoping for. So, use size(P1, 1) and size(v, 1)
The reason I stopped at CheckConc is that the function is wrong. It only performs N-1 checks for a N sided polygon so is missing one vertex in the concavity check. I've not tried to understand the math you're doing. There are more efficient algorithm to check that concavity. See stackoverflow and math.stackexhange. If you're guaranteed that your polygon is not self-intersecting that latter one would be best. It can be implemented without any loop in matlab.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aditya Patil
Aditya Patil 2020년 3월 27일
Inside findwidth function, you have modified the variable D, which is a global variable. This creates the issue.
Instead, pass D as a parameter to the function, as follows ( parameter named E in the code below)
function width=findwidth(P11,ncc, E)
if ncc~=0
[k1,av] = convhull(P11);
CP=[P11(k1,1),P11(k1,2)];
else
CP=P11;
end
for q=1:(length(CP)-1)%:(length(CP)-1)
for z=1:(length(CP)-1)
E(q,z)=Seg_point_dis(CP(z,:),CP(z+1,:),CP(q,:));
end
end
D1=max(E,[],2) ;
width=min(D1);
end
  댓글 수: 1
farah fadel
farah fadel 2020년 3월 28일
I am still getting different answer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Bounding Regions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by