143 views (last 30 days)

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

Aditya Patil
on 27 Mar 2020 at 9:16

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

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 16 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812851

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812851

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812858

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812858

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812876

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_812876

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813789

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813789

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813832

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813832

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813833

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813833

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813845

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813845

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813851

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813851

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813854

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_813854

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_815566

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_815566

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816079

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816079

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816181

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816181

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816184

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816184

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816190

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816190

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816193

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816193

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816217

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/511945-different-answers-for-the-same-algorithm#comment_816217

Sign in to comment.