Create a Matrix with Euclidian Distances between Coordinates

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by