Distance Between Two Points in Matlab

丶灬走出姿态 提交于 2019-12-01 20:14:09

square is not for squaring a value, it returns the values of the square wave.

You can use pdist2 to compute pairwise distance between two sets of observations as follows:

X = randn(200, 2);
Y = randn(3, 2);
D = pdist2(X,Y,'euclidean'); % euclidean distance

The square function is not what you want (it generates a square wave).

To calculate the square of a number, use the ^ operator:

x = 3;
y = x ^ 2;
disp(y);  % Prints 9
tic
A = pdist2( X,  X);
toc
% method 2
tic
n = size(X, 1);
idx = repmat(1:n, n, 1);
D = sqrt(sum((X(idx,:)-X(idx',:)).^2, 2));
D = reshape(D, n, n);
toc
find(A-D)
Elapsed time is 0.021950 seconds.
Elapsed time is 0.043413 seconds. % and add your satisfaction approximately -0.02 seconds
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!