3 views (last 30 days)

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!

Subhadeep Koley
on 6 Nov 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.