calculate distance within matlab

x=[1,3,5];% x coordinate
y=[2,2,1];% y
z=[1,2,3];% z

example 1,

x=repmat(x',1,numel(x));% or x=x*ones(1,numel(x));
x=(x-x')^2;
y=repmat(y',1,numel(y));
y=(y-y')^2;
z=repmat(z',1,numel(z));
z=(z-z')^2;
d=sprt(x+y+z);%distance

example 2,

t=cat(2,x',y',z');
t=t*t';
n=size(t,1);
d=sqrt(-2*t+diag(t)*ones(1,n)+ones(n,1)*diag(t)');
%(x1-x2)^2+(y1-y2)^2+(z1-z2)^2 = -2(x1x2+y1y2+z1z2)+x1^2+y1^2+z1^2+x2^2+y2^2+z2^2


你可能感兴趣的:(calculate distance within matlab)