# Plotting lines between X,Y coordinates after filtering by calculated distance

3 views (last 30 days)
Claire on 3 Nov 2019
Commented: Subhadeep Koley on 6 Nov 2019
I've calculated the distance between my X,Y coordinates (stored in "coord" a 220x2 double). Accordingly, "distance" is a 220x220 double. See the successful code in this first block.
coord = horzcat(X,Y1);
[mmm nnn] = size(coord);
nnn = mmm;
distance = zeros(mmm,nnn);
% slope = zeros(mmm, nnn);
for i = 1 : mmm
for j = 1 : nnn
scale = 8.6719;
distance(i, j) = (sqrt((coord(i, 1) - coord(j, 1)) ^ 2 + ...
(coord(i, 2) - coord(j, 2)) ^ 2))/scale;
% slope(i,j) = (coord(i, 2) - coord(j, 2))/(coord(i, 1) - coord(j, 1));
end
end
What I need to do now is output a visual confirmation. I'd like to only plot lines with a distance less than 10. Since this is just a visual confirmation, I'd like to only do it with the first column of "distance" (the distance calculations with respect to the first X,Y coordinate).
This is what I tried to do in order to get my visual confirmation, but it nearly crashed my computer. Any suggestions or links to existing forums are greatly appreciated. Thank you!
for i = 1 : mmm
for j = 1 : nnn
scale = 8.6719;
distance(i, j) = (sqrt((coord(i, 1) - coord(j, 1)) ^ 2 + ...
(coord(i, 2) - coord(j, 2)) ^ 2))/scale;
% slope(i,j) = (coord(i, 2) - coord(j, 2))/(coord(i, 1) - coord(j, 1));
distance_example = distance(:,1);
for p = 1:length(distance_example)
if distance_example < 10
plot(distance, 'bs-', 'LineWidth', 2, 'Color', 'b');
end
end
end
end

Subhadeep Koley on 6 Nov 2019
Hi, your code is almost correct. However, plotting like this is very inefficient, which probably is the reason why your computer crashed. As, it generates a huge number of individual graphic objects instead of just one.
You should just store the required distances from your loop in an array and then do a single plot instruction after the loop. Refer the code below.
coord = horzcat(X,Y1);
[mmm, nnn] = size(coord);
nnn = mmm;
distance = zeros(mmm,nnn);
% slope = zeros(mmm, nnn);
for i = 1 : mmm
for j = 1 : nnn
scale = 8.6719;
distance(i, j) = (sqrt((coord(i, 1) - coord(j, 1)) ^ 2 + ...
(coord(i, 2) - coord(j, 2)) ^ 2))/scale;
% slope(i,j) = (coord(i, 2) - coord(j, 2))/(coord(i, 1) - coord(j, 1));
end
end
% For Visualization only
% Extract the first column
distance_example = distance(:,1);
% Extract elements from 'distance_example' which are less than 10
distance_example_th = distance_example(distance_example<10);
% Plot 'distance_example_th'
figure; plot(distance_example_th, 'bs-', 'LineWidth', 2, 'Color', 'b');
Hope this helps!

Claire on 6 Nov 2019
Thank you, this does help!
However, I would like the distances to be plotted on top of the original X,Y coordinates (magenta, stored in coord). For some reason, this code generates the correct distances but with arbitrary X,Y coordinates (blue coordinates in top left hand corner of first image, shown zoomed in on second image).
Since these coordinates are referred to in the loop, that is why I initially included the "distance_example" there. Although, I do understand it is inefficient
Subhadeep Koley on 6 Nov 2019
Yes Claire, this is expected because distance_example_th is a separate vector here. When you're plotting it on top of coord it is just getting plotted from the start irrespective of the coordinates. As, in the variable distance_example_th we are taking only the valus (not the index) of the distances which are less than 10. However, I think plotting the distances separately in a new plot will serve the purpose of visual confirmation.