필터 지우기
필터 지우기

calculating minimum distance in a circular manner

조회 수: 3 (최근 30일)
Shamsuddeen Abdullahi
Shamsuddeen Abdullahi 2019년 6월 7일
편집: Adam Danz 2020년 8월 26일
Hello, Pls some one should help.
I want to find a minimum distance between set of points.
suppose there are 3 points, p1(xi,y1), p2(x2,y2) and p3(x3,y3).
I want to find the following distances and select the least among them:
distance1= distance from p1 to p2 + distance from p2 to p3
distance2=distance from p2 to p3 + distance from p3 to p1
distance3=distance from p3 to p1 + distance from p1 to p2
wanted_distance=min(distance1,distance2, distance3).
I used the ffg code but im getting an error msg
for i=1:1:3
d(i)=sqrt( (x(i) - x(i+1) ).^2 + (y(i) - y(i+1) ).^2 )
end.
As expected, I got an error msg. I guess using two (2) for loops will accomplish the task, but couldnt figure-out the appropraite syntax.
Pls, Assist

답변 (1개)

Raghunandan V
Raghunandan V 2019년 6월 7일
편집: Raghunandan V 2019년 6월 7일
Hi,
Here is a solution
X = [1 2 3];
Y = [2 3 4];
dist = zeros(length(Y),1);
for a = 1: length(Y)
%eucliedian distance
dist(a) = sqrt((X(1) - X(2))^2 + (Y(1) - Y(2))^2) + sqrt((X(2) - X(3))^2 + (Y(2) - Y(3))^2);
% left shift by 1
X = circshift(X ,[1 -1])
Y = circshift(Y ,[1 -1])
end
result = min(dist);
Corrected version
  댓글 수: 2
Shamsuddeen Abdullahi
Shamsuddeen Abdullahi 2019년 6월 7일
this only gives the distances:
1) from 1 to 2
2) 2 to 3
3) 3 to 1
unlike what I need (as explained above)
Raghunandan V
Raghunandan V 2019년 6월 7일
Oh!, sry. I will edit it

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by