How to create a distance matrix?

5 次查看(过去 30 天)
Evan Bakalars
Evan Bakalars 2021-3-11
回答: David Hill 2021-3-11
Hello, so I am running into a brick wall over and over again trying to solve this part of a problem.
I have a matrix of different coordinates where the first row is the x-coordinate and the second row is the y-coordinate as listed here:
coord = [0 10 20 10 -20;0 0 10 20 -40]; This makes coordinate 1 =(0,0), coordinate 2 = (10,0), coordinate 3 = (20,10), and so on.
I know what the distance formula is, my struggle resides in how to get a matrix where every coordinate combination has its distance calculated, so if you looked at the distance matrix (I'll call it distMatrix) each element is the distance between its first coordinate number (the number of the row its in) and the second coordinate number (the number of the column it is in).
This would be so that distMatrix(2,3) is the distance between coordinate 2 and coodinate 3, or distMatrix(4,4) is the distance between coordinate 4 and coordinate 4 (which would be zero).
I greatly appreciate any help or guidance on this question

回答(1 个)

David Hill
David Hill 2021-3-11
coord = [0 10 20 10 -20;0 0 10 20 -40];
c=nchoosek(1:size(coord,2),2);
dist=zeros(1,size(c,1));
for k=1:size(c,1)
dist(k)=norm(coord(:,c(k,2))-coord(:,c(k,1)));
end

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by