Distance between points in a table
古いコメントを表示
Hello all,
I have a table containing the (x,y) coordinates and I need to find the distance between each point and save it as an array. Could anybody please guide me?
Thanks in advance.
3 件のコメント
Geoff Hayes
2020 年 11 月 16 日
K1495 - what can you tell us about your table? Can you convert it to an mx2 array for each of your m points? If you have the Statistics and Machine Learning Toolbox, you could probably use pdist.
Adam Danz
2020 年 11 月 16 日
or pdist2
KK14
2020 年 11 月 19 日
回答 (1 件)
>I have a table containing the (x,y) coordinates and I need to find the distance between each point [without using pdist | pdist2]
Calculate distances row-wise
Create input table
rng('default')
T = array2table(rand(8,2).*10, 'VariableNames',{'X','Y'})
Add a column Dist that shows the distance between rows n and n-1.
T.Dist = [NaN; sqrt(diff(T.X).^2 + diff(T.Y).^2)]
Calculate pairwise distances
Create input table
rng('default')
T = array2table(rand(8,2).*10, 'VariableNames',{'X','Y'})
Create square matrix of pairwise distances
[xi, yi] = meshgrid(1:numel(T.X), 1:numel(T.Y));
D = reshape(sqrt(diff(T.X([xi(:),yi(:)]),1,2).^2 + diff(T.Y([xi(:),yi(:)]),1,2).^2),size(xi))
D(i,j) is the distance between points T(i,:) and T(j,:).
カテゴリ
ヘルプ センター および File Exchange で Classification Trees についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!