how to calculate distance between one and other points?
5 views (last 30 days)
Show older comments
I have 6 sets of data (x,y,z,grid_x,grid_y,grid_z), xyz have 100 points and it is detemine by me randomly, grid_xyz is the detemining the grid, from(0,0) to (100,100) with 2m intervals, which means grid_x = (0,2,4,6,..), grid_y= (0,2,4,6,..), I need to calculate the distance between (x,y) to (grid_x,grid_y), that is calculate each (grid_x,grid_y) to all the point (x,y), for instance (0,0) to (x1,y1) (x2,y2) (x3,y3)...(x100,y100), then (0,2) to (x1,y1) (x2,y2) (x3,y3)...(x100,y100) and so on. how to calculate this? thank you so much!
0 Comments
Accepted Answer
Image Analyst
on 25 Feb 2022
You can use pdist2()
xyz1 = [x(:), y(:), z(:)];
xyz2 = [grid_x(:), grid_y(:), grid_z(:)];
distances = pdist2(xyz1, xyz2);
Now you have the distance of every point in xyz to every point in xyz2. If you want to find the closest point to xyz that is in xyz2, you can examine the rows for the min distance.
3 Comments
Image Analyst
on 25 Feb 2022
What do you mean haven't come up? Take the semicolon off and it should spit out a 100 row by 51 column matrix to the command window. Or else look at it in the variable editor (Double click on distances in the workspace panel).
More Answers (0)
See Also
Categories
Find more on Multirate Signal Processing 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!