Calculating the distance between every two points of a list?

11 次查看(过去 30 天)
Hello everybody
I have a trajectory like this: We assume that each red star marker can broadcast its coordinate to the green circle markers which are located within a radius of 5 units from its own position.I selected a list of n red points for each green marker according to the above explanation.
How can I calculate the euclidean distance between every two red points of the list and the select the maximum euclidean distance corresponding red points? Thanks.
%%Network Setup
anchor_num=1; % Number of anchor node
node_num=20; % Total nodes
length1=70; % Area length
anchor_x=0; % Intial position of anchor x coordinate
anchor_y=0; % Intial position of anchor y coordinate
anchormove=[];% Anchor trajectory
width=40; % Area width
r = 30;
A = zeros(0,2);
B = zeros(0,2);
C = zeros(0,2);
D = zeros(0,2);
north = [ 0 6.9];
east = [ 6.9 0];
south = [ 0 -6.9];
west = [-6.9 0];
order = 4;
for n = 1:order
AA = [B ; north ; A ; east ; A ; south ; C];
BB = [A ; east ; B ; north ; B ; west ; D];
CC = [D ; west ; C ; south ; C ; east ; A];
DD = [C ; south ; D ; west ; D ; north ; B];
A = AA;
B = BB;
C = CC;
D = DD;
end_
% Plot network trajectory
%Mtrix A contains the coordinate of red markers.
A = [0 0; cumsum(A)]
p=plot(A(:,1),A(:,2))
title('Plot of Hilbert trajectory');
set(p,'Color','magenta ','LineWidth',2);
axis([0 100 0 100]);
hold on
% x and y are the coordinates of green markers
x=rand(1,100)*100;
y=rand(1,100)*100;
scatter(x,y)
anchormove(1,:)=A(:,1)'
anchormove(2,:)=A(:,2)'
idx=length(anchormove(1,:));
for i=1:idx-1
% Plot the moving anchor node
Ax=anchormove(1,i);
Ay=anchormove(2,i);
plot(Ax,Ay,'r*');
% Plot transmission range of the anchor node
axis([0 100 0 100])
% hold on
pause(0.1)
%hold off
% turn the two x and y vectors into [x y] column format.
GreenPoints = [x;y].';
% get indexes of the points A for each Green point within 5 distance
idx = rangesearch(A,GreenPoints,5);
end

采纳的回答

Image Analyst
Image Analyst 2017-7-22
Not sure I follow your terminilogy, but if redListX and redListY are the list of red points (as column vectors) within 5 units of your green point, you can find the distances between every red point with every other red point like this:
redPairWiseDistances = pdist2([redListX, redListY], [redListX, redListY]);
Then find the max like this
[maxDistance, linearIndex] = max(redPairWiseDistances(:))
Note, this is the max distance between the any pairs of red points on subset of red points on your list, not the distance from any of those from the green point, which you did not ask for.
  1 个评论
Zeinab Ahmadi93
Zeinab Ahmadi93 2017-7-25
编辑:Zeinab Ahmadi93 2017-7-25
Thanks for your reply.
I need the coordinate of the two points that their distance is maximum.But I don't know how to define it.
For example B(x1,y1) and C(x2,y2) has the maximum distance that we found using the above codes. I need to find the below parameters.
deltaX= x2-x1 deltaY= y2-y1

请先登录,再进行评论。

更多回答(2 个)

James Tursa
James Tursa 2017-7-22

Image Analyst
Image Analyst 2017-7-25
See this demo code and hopefully it will be clear. It finds the pair of points, one from set #1 and one from set #2, that are farthest apart.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
% Make 50 points in set #1
numPoints1 = 50;
x1 = rand(numPoints1, 1);
y1 = rand(numPoints1, 1);
% Make 75 points in set #2
numPoints2 = 75;
x2 = rand(numPoints2, 1);
y2 = rand(numPoints2, 1);
% Plot set 1 in red
plot(x1, y1, 'r.', 'MarkerSize', 13);
% Plot set 2 in blue
hold on;
plot(x2, y2, 'b*', 'MarkerSize', 13);
grid on;
% Find distances between every point in set 1 to every point in set #2.
% Return distance and index of farthest from every point in set #1.
[distances, index] = pdist2([x1, y1], [x2, y2], 'euclidean', 'largest', 1)
% Find max distance
[maxDistance, indexOfMax] = max(distances)
index2 = indexOfMax
index1 = index(indexOfMax)
% Draw a line between the two points farthest apart.
plot([x1(index1), x2(index2)], [y1(index1), y2(index2)], 'mo-', 'LineWidth', 2, 'MarkerSize', 20);
legend('Set #1', 'Set #2', 'Farthest apart');

类别

Help CenterFile Exchange 中查找有关 3-D Scene Control 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by