Create a Matrix with Euclidian Distances between Coordinates

1 view (last 30 days)
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!

Accepted Answer

KSSV
KSSV on 20 Sep 2022
A = rand(8,2,3) ;
B = permute(A,[1 3 2]);
B = reshape(B,[],size(A,2),1) ;
iwant = squareform(pdist(B))

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!