l2_distance.m
来自「无线传感器节点定位算法」· M 代码 · 共 34 行
M
34 行
function d = L2_distance(a,b,df)
if (nargin < 2)
error('Not enough input arguments');
end
if (nargin < 3)
df = 0; % by default, do not force 0 on the diagonal
end
if (size(a,1) ~= size(b,1))
error('A and B should be of same dimensionality');
end
if ~(isreal(a)*isreal(b))
disp('Warning: running distance.m with imaginary numbers. Results may be off.');
end
if (size(a,1) == 1)
a = [a; zeros(1,size(a,2))];
b = [b; zeros(1,size(b,2))];
end
aa=sum(a.*a); bb=sum(b.*b); ab=a'*b;
d = sqrt(repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab);
% make sure result is all real
d = real(d);
% force 0 on the diagonal?
if (df==1)
d = d.*(1-eye(size(d)));
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?