Create a Matrix with Euclidian Distances between Coordinates

1 回表示 (過去 30 日間)
Gabriel Germann
Gabriel Germann 2022 年 9 月 20 日
コメント済み: Gabriel Germann 2022 年 9 月 20 日
I have a 8x2x3 Matrix called "Dot_Space" with X and Y coordinates.
8 is the number of dots per row.
2 are the respective X and Y coordinates.
3 is the number of rows.
Now I would like to create a matrix that includes all euclidian distances between the dots.
% Euclidian distance between two points in two dimensional space: d(p,q) = sqrt((q1-p1)^2 + (q2-p2)^2)
for i = 1:length(Dot_Space)
for j = 1:length(Dot_Space)
for n = Number_of_Rows
if i ~= j
Distance(i,j,n) = sqrt((Dot_Space(i,1,n)-Dot_Space(j,1,n))^2 + (Dot_Space(i,2,n) Dot_Space(j,2,n))^2);
end
end
end
end
I assume that the amount of possible combinations/distances must exceed the size of the returned matrix and I forgot to implement something crucial. Someone with more experience in MatLab will probably be able to point out my mistake?!
Thank you very much!

採用された回答

KSSV
KSSV 2022 年 9 月 20 日
A = rand(8,2,3) ;
B = permute(A,[1 3 2]);
B = reshape(B,[],size(A,2),1) ;
iwant = squareform(pdist(B))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by