How to create a distance matrix?

5 views (last 30 days)
Evan Bakalars
Evan Bakalars on 11 Mar 2021
Answered: David Hill on 11 Mar 2021
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

Answers (1)

David Hill
David Hill on 11 Mar 2021
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

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!