cossimilarity.m

来自「一个关于数据聚类和模式识别的程序,在生物化学,化学中因该都可以用到.希望对大家有」· M 代码 · 共 21 行

M
21
字号
function cosine = cosSimilarity(mat1, mat2)
% cosSimilarity: Cosine of the angles betweeen two set of vectors
%	Usage: cosine = cosSimilarity(mat1, mat2)
%	Returns the cosine of the angles between two set of vectors mat1 and mat2.
%	The element at row i and column j of the return matrix is the cosine of the angle between
%	column i of mat1 and column j of mat2.

%	Roger Jang, 19971227, 20080915.

if nargin<1, selfdemo; return; end
if nargin<2, mat2=mat1; end

leng1 = (sum((mat1).^2).^.5);
leng2 = (sum((mat2).^2).^.5);
cosine = (mat1.'*mat2)./(leng1.'*leng2);

% ====== Selfdemo
function selfdemo
mat1=rand(3);
mat2=rand(3);
cos=feval(mfilename, mat1, mat2)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?