Nearest Neighbor XY plot
2 views (last 30 days)
Show older comments
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 Comments
DGM
on 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?
Answers (1)
Image Analyst
on 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]
B = [2, 4; 8, 10; 12.9,17.1]
% 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)
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!