Find points within a certain distance of a point
23 views (last 30 days)
Show older comments
I have a 100 by 2 matrix of 100 x and y coordinates. For each x, y- coordinates I want to compute distance from every other x, y-coordinate within the matrix and create a 2-D array with the points that are within a distance of 100 from each point. Any help? Thank you in advance!! This is what I have so far, the result is the distance from just the first set of points. I want to loop through each of the points.
A= [40.8504 171.3564
587.0150 277.7693
534.6382 201.7993
126.7681 271.1941
568.2468 511.1568
239.4433 273.8657
356.6801 596.7002
502.6793 249.0090
417.0619 137.1853
72.5468 584.0626
213.3594 217.7381
304.1814 500.0268
343.7102 310.6837
487.7260 131.2077
31.6978 458.0418
244.3493 346.3767
70.9269 445.2531
417.1997 341.9932
146.6901 530.5202
35.8071 190.4143
592.4875 275.6106
183.8519 296.2981
267.6878 371.1306
494.7850 153.3958
439.6924 48.7718
367.2361 189.6672
286.4973 266.1874
337.3543 100.3856
369.7438 576.1130
133.3710 248.8937
427.1103 564.4894
177.1575 367.4444
413.4330 335.9265
176.4288 331.0766
170.1338 490.0941
549.3505 30.8605
510.4268 45.3470
432.7743 499.5853
415.8379 246.3115
16.7261 441.5396
52.1727 9.6495
180.4457 92.3413
205.7786 489.2302
525.2715 477.1678
153.2615 595.6637
77.1528 218.9533
89.2199 170.4176
207.7396 255.6136
510.8726 442.3980
45.6925 169.6155
425.2625 157.6051
493.0561 385.6737
288.9014 283.9012
520.7237 308.7363
82.5716 38.1560
181.9178 364.0340
147.4173 492.0537
54.6747 60.4496
131.1049 599.3833
217.6165 202.4072
422.8320 12.2127
595.2158 425.1874
434.7470 217.9533
575.9956 317.3215
363.2477 381.2383
451.9813 549.5836
211.6979 134.7063
498.0325 258.4985
215.7108 339.1568
167.4092 542.0809
39.9130 272.1907
197.9183 436.0215
571.8909 300.4348
277.1690 571.4164
294.3354 262.1698
288.4176 450.6247
328.1266 480.3169
23.2771 514.6075
323.5743 188.7017
193.8247 61.1335
290.8283 421.8025
62.0086 86.4285
582.8345 43.5660
174.8900 539.8010
237.2108 578.1350
164.1080 549.2339
449.9186 192.6590
148.5850 343.0218
135.7795 465.0395
475.8121 16.6119
502.5542 168.8421
416.1112 267.7820
533.3713 252.3326
448.7790 77.0000
319.4817 436.2359
497.7837 307.7780
170.1447 530.0457
510.7536 432.1919
369.4765 234.9124
535.5857 338.3505]
B=A
i=1:100
A(i,1)
A(i,2)
B(1,:);
B(2,:);
distances = sqrt(( (A(i,1 )- B(1,:)).^2 + (A(i,2) - B(2,:)).^2 )) ;
0 Comments
Accepted Answer
Image Analyst
on 5 Dec 2017
You can use pdist2() in the stats toolbox to compute the distance of every point to every other point.
distances = pdist2(A, A);
Find which points are within 100 of each other
closePoints = distances < 100;
closePoints is a 100 by 100 array saying whether each point is close to any other point. So, for example, considering points 3 and 5, if point 3 and point 5 are within 100 of each other, then the (3,5) location of closePoints will be true. If they are farther away, then it will be false. Same for any other location in the array. So you know just by looking at the closePoints array, which points are close or not close to each other.
More Answers (2)
Roger Stafford
on 5 Dec 2017
You can do this:
n = size(A,1);
[I,J] = meshgrid(1:n);
I = I(:); J = J(:);
D = reshape(sqrt((A(I,1)-A(J,1)).^2+(A(I,2)-A(J,2)).^2),n,n);
D will be an n-by-n matrix in which the i-th row and j-th column will be the distance between points A(i,:) and A(j,:).
I'm not sure just what you meant by "points that are within a distance of 100 from each point." It would either be
T1 = D<=100;
where T1 is an n-by-n matrix of true values for all i,j pairs of points that are no more distant than 100, or
T2 = all(D<=100,2);
where T2 is a column vector which is true for all points in A each of which is simultaneously no greater distance than 100 from all other points.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!