Nearest Neighbor XY plot

2 Ansichten (letzte 30 Tage)
Keturah Palma
Keturah Palma am 7 Feb. 2022
Kommentiert: Keturah Palma am 7 Feb. 2022
A = [1.5, 5.4; 9.2, 3.3; 6.9, 11.1]
B = [2, 4; 8, 10; 12.9,17.1]
5 radius
  4 Kommentare
DGM
DGM am 7 Feb. 2022
What nearest neighbor function? Are you referring to this?
If so, I'm not really familiar with network theory tools. As A and B are non-integers, they aren't indices; what are they?
Keturah Palma
Keturah Palma am 7 Feb. 2022
is there another way to do it?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 7 Feb. 2022
Not sure what function you're talking about. If you just want to find points in Set A that are closer than 5 to points in Set B, you can do this:
markerSize = 40;
A = [1.5, 5.4; 9.2, 3.3; 6.9, 11.1]
A = 3×2
1.5000 5.4000 9.2000 3.3000 6.9000 11.1000
B = [2, 4; 8, 10; 12.9,17.1]
B = 3×2
2.0000 4.0000 8.0000 10.0000 12.9000 17.1000
% Plot A
plot(A(:, 1), A(:, 2), 'r.', 'MarkerSize', markerSize);
hold on;
% Plot B
plot(B(:, 1), B(:, 2), 'b.', 'MarkerSize', markerSize);
grid on;
distances = pdist2(A, B)
distances = 3×3
1.4866 7.9630 16.3355 7.2339 6.8066 14.2874 8.6267 1.5556 8.4853
for row = 1 : size(distances, 1)
[r, c] = find(distances(row, :) <= 5);
for k = 1 : length(c)
xCoords = [A(row, 1), B(c(k), 1)];
yCoords = [A(row, 2), B(c(k), 2)];
line(xCoords, yCoords, 'Color', 'm', 'LineWidth', 2)
fprintf('Drawing line between (%.1f, %.1f) and (%.1f, %.1f). The distance is %.3f.\n', ...
A(row, 1), A(row, 2), B(c(k), 1), B(c(k), 2), distances(row, c(k)))
end
end
Drawing line between (1.5, 5.4) and (2.0, 4.0). The distance is 1.487.
Drawing line between (6.9, 11.1) and (8.0, 10.0). The distance is 1.556.

Community Treasure Hunt

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

Start Hunting!

Translated by