필터 지우기
필터 지우기

Computing distance between two set of points (under vectorized approach)

조회 수: 2 (최근 30일)
SC
SC 2019년 4월 3일
댓글: madhan ravi 2019년 4월 3일
I have two matrices (two set of p-dimensional points), X1 and X2, which are n*p and m*p matrices. I want the final output Dist, whose i-j entries is the distance between row_i of X1 and row_j of X2 (i.e. inner product of the difference).
I want to do this in the vectorization way (avoid using for loop), since the actual matrix size is very big.
For p=2, the following can do the job (X1 is named as XY1 and X2 is named as XY2 in the example, since the dimension is 2 only):-
How can I do that, say for p=10? Many thanks!
function Dist=dist_matrix(XY1,XY2)
% input: both XY1 and XY2 have 2 columns
% input: XY1 and XY2 are not necessarily having same number of rows
X1=XY1(:,1);
Y1=XY1(:,2);
X2=XY2(:,1);
Y2=XY2(:,2);
f = @(hori,verti)(hori-verti);
Dist_X = bsxfun(f,X2',X1);
Dist_Y = bsxfun(f,Y2',Y1);
Dist=sqrt(Dist_X.^2+Dist_Y.^2);
end
  댓글 수: 3
SC
SC 2019년 4월 3일
Thanks for your reply. But what's your exact meaning? For example, how to do the arithmetic operations directly for the below example?
A = [8; 17; 20; 24];
B = [0 10 21];
bsxfun(@(x,y)x+y,A,B)

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by