The distance between two points in Matlab

I have 2 vectors, each of which is 200 * 2, and the other is 3 * 2. All of them are points in the Cartesian coordinate system. I want to calculate the distance between the first 200 and the other three points and save them in a vector. I use this function:

for i=1:cur for j=1:200 L(j,i)=sqrt(square(P2(i,1)-C(j,1))+square(P2(i,2)-C(j,2))) end end 

where cur is 3, P2 is the vector 3 * 2, and C is 200 * 2. Now the results that I get are completely wrong, but I can not understand the problem with this. Any help would be good if there is another way to figure it out, I would appreciate. On the way for more information;

 P2 = [2 -2;3 -5 ; -1 3]; 

and the other -

 theta = linspace(0,2*pi,200)'; %' unitCircle = [cos(theta) sin(theta)]; C = zeros(numel(theta),2,num); 
+4
source share
3 answers

square not a squared value, it returns a square wave value.

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

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

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 
+1
source
 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(AD) Elapsed time is 0.021950 seconds. Elapsed time is 0.043413 seconds. % and add your satisfaction approximately -0.02 seconds 
0
source

Source: https://habr.com/ru/post/1381709/


All Articles