How to create a distance matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Earth and Planetary Science에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!