📄 cosine.m
字号:
function d = cosine(a,b,df)% EUCLIDEAN - computes Euclidean distance matrix%% E = euclidean(A,B)%% A - (DxM) matrix % B - (DxN) matrix% df = 1, force diagonals to be zero; 0 (default), do not force% % Returns:% E - (MxN) Cosine distances between vectors in A and B%%% Description : % This fully vectorized (VERY FAST!) m-file computes the % Euclidean distance between two vectors by:%% ||A-B|| = (1 - A.B/|| A |||| B||)%% Example : % A = rand(400,100); B = rand(400,200);% d = distance(A,B);%% Author: vikas Sindhwani% June 2004if (nargin < 2) error('Not enough input arguments');endif (nargin < 3) df = 0; % by default, do not force 0 on the diagonalendif (size(a,1) ~= size(b,1)) error('A and B should be of same dimensionality');endif ~(isreal(a)*isreal(b)) disp('Warning: running distance.m with imaginary numbers. Results may be off.'); endif (size(a,1) == 1) a = [a; zeros(1,size(a,2))]; b = [b; zeros(1,size(b,2))]; endaa=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);d=ones(size(a,2),size(b,2))-(1./sqrt(kron(aa',bb))).*ab;% make sure result is all reald = real(d); % force 0 on the diagonal? if (df==1) d = d.*(1-eye(size(d)));end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -