Distance between ALL(all combinations) points of matrix

조회 수: 7 (최근 30일)
Lidziya Semeniuk
Lidziya Semeniuk 2019년 10월 19일
답변: Rik 2019년 10월 19일
How can i have a vector which consists of all the distances betweeen all points of matrix? I have 3D matrix B = 151*3 with points coordinates (X,Y,Z are columns). Possible combination of all this points should be 11325(n = 151,k=2). Snímek obrazovky 2019-10-19 v 14.33.27.png
`I can calculate the distance between 2 3d points like this:
dist = norm(B(1,:,:)-B(2,:,:))
but unfortunately my for loop doesn't work:
n = size(B,1)
for k = 1:n-1
for a = k:n-1;
Z1 = B(k,:,:);
Z2 = B(a+1,:,:);
dist = norm(Z2 - Z1);
end
end
it returns only one number , but i want to have a vector with all distances, so i can make a histogram from it.
Where is the mistake of the for loop? Or can i solve this with repmat function?
Thank you!

채택된 답변

Rik
Rik 2019년 10월 19일
Note that you are not calculating only unique combinations. The reason you are only getting a single value is that you are not indexing your output variable.
What you should do is generate the matrix of indices of the combinations and then loop through that matrix. You can use nchoosek to create the matrix.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by