getting distance between each of the meshgrid point from predefined points in the plot

9 views (last 30 days)
I am trying to get distance of each point in a meshgrid(1:180, 1:180) from certain predefined points stored in "busc" matrix given below
x coor y coord
59.2000 160.0000
80.6500 160.0000
58.9500 133.0000
77.7500 133.0000
80.2000 122.0000
54.3500 118.0000
67.3000 121.0000
40.0500 118.0000
42.8500 108.0000
89.6000 76.8000
54.5500 108.0000
124.5000 128.0000
141.5000 128.0000
124.5000 147.0000
124.5000 160.0000
125.5000 116.0000
145.5000 116.0000
155.0000 160.0000
155.0000 148.0000
144.5000 89.2000
90.7500 56.1000
69.5000 56.6000
141.0000 69.1000
106.0000 56.1000
50.3000 57.0000
50.1500 68.8000
48.0000 27.5000
36.0000 142.0000
75.9000 27.2000
100.7500 26.6000
for i=1:180
for j=1:180
for k=1:30
dist(i,j)=((busc(k, 1) - i)^2+ (busc(k, 2) - j)^2)^.5;
end
end
end
The dist (1,1) should have 169.317 as the value but it is coming as 102. Same is the case with all other points... why so?
BASICALLY I need to get the cumulative sum of all the distances of each of these mashgrid points from the abovementioned 30 predefined points.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 15 Nov 2011
The problem is at your inner loop of k. As of now, dist(i,j) is re-set at every iteration of k so the result is the same as for k==30.
Maybe you need to initialize dist=zeros(180) outside of the loop and then do the accumulation inside the for-loop?
  2 Comments
Arundhatee Talukdar
Arundhatee Talukdar on 15 Nov 2011
Like This? It is giving me 0 in all elements
for i=1:180
for j=1:180; dist=zeros(180) ;
for k=1:30
dist(i,j)=((busc(k, 1) - i)^2+ (busc(k, 2) - j)^2)^.5;
% vvolt=r.bus(k,8)/sum(dist,2);
end
end
end
Fangjun Jiang
Fangjun Jiang on 15 Nov 2011
Move dist=zeros(180) to the beginning, outside of the i loop. And change to this:
dist(i,j)=dist(i,j)+((busc(k, 1) - i)^2+ (busc(k, 2) - j)^2)^.5;

Sign in to comment.


bym
bym on 15 Nov 2011
I don't fully understand, but you might want to look at changing this line to include the k dimension
dist(i,j,k)=((busc(k, 1) - i)^2+ (busc(k, 2) - j)^2)^.5;

Community Treasure Hunt

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

Start Hunting!