
Plotting lines between X,Y coordinates after filtering by calculated distance
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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!
2 Comments
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.
More Answers (0)
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!