Distance between two set of points in space

조회 수: 8 (최근 30일)
andrea
andrea 2022년 1월 26일
댓글: David Hill 2022년 1월 27일
Hello,
So I have a set of points in a matrix A 10x3 ( i.e 10 points with x,y,z coordinates) and another set of points in a matrix B 10x3. I want to find the distance between each point of A with each point of B whitout using a for loop.
Normally I would fix a point in A and compute all the distances with the points in B and then choose another point in A and do the same and so on. Is there a way to do it directly from the matrix A and B whitout a loop ?

답변 (2개)

Matt J
Matt J 2022년 1월 26일
distances = pdist2(A,B);

David Hill
David Hill 2022년 1월 26일
Why no loop? Below is oneliner but has an internal loop.
C=arrayfun(@(x)vecnorm((A(x,:)-B)'),1:size(A,1),'UniformOutput',false);
  댓글 수: 2
andrea
andrea 2022년 1월 27일
Yes thank you for the answer, that's nice. I thought that there could have been a better way without using for loop (some kind of vectorization) because in the case I have lots of points a for loop starts to be very slow.
David Hill
David Hill 2022년 1월 27일
Matt J answer is better.
pdist2(A,B)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by