Matlab中两点之间的距离 [英] Distance Between Two Points in Matlab

查看:1190
本文介绍了Matlab中两点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个向量,一个向量的尺寸是200 * 2,另一个是3 * 2.它们都是笛卡尔坐标系中的点.我想计算前200个点与其他3个点之间的距离,并将它们存储在向量中.我正在使用这样的功能;

I have 2 vectors one is 200*2 in dimension and other is 3*2.All of them are points in a cartesian coordinate system. I want to calculate the distance between the first 200 and the other 3 points and store them in a vector. I'm using a function like this;

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

其中cur是3,P2是3 * 2向量,C是200 * 2.现在我得到的结果是完全错误的,但我无法找出问题所在.如果有另一种计算方法,我将不胜感激.通过这种方式,您可以获取更多信息;

where cur is 3 , P2 being the 3*2 vector and C being the 200*2.Now the results i get are completely wrong but I cannot figure out the problem in that. Any help would be good , if there is another way to compute it i would appreciate.By the way for more information ;

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

另一个是

theta = linspace(0,2*pi,200)';   %'
unitCircle = [cos(theta) sin(theta)];
C = zeros(numel(theta),2,num);

推荐答案

square 不用于平方值,它返回方波的值.

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

您可以使用 pdist2 计算两个之间的成对距离多组观察结果如下:

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

这篇关于Matlab中两点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆