How to compute cosine similarity using two matrices

跟風遠走 提交于 2020-01-03 11:40:12

问题


I have two matrices, A (dimensions M x N) and B (N x P). In fact, they are collections of vectors - row vectors in A, column vectors in B. I want to get cosine similarity scores for every pair a and b, where a is a vector (row) from matrix A and b is a vector (column) from matrix B.

I have started by multiplying the matrices, which results in matrix C (dimensions M x P).

C = A*B

However, to obtain cosine similarity scores, I need to divide each value C(i,j) by the norm of the two corresponding vectors. Could you suggest the easiest way to do this in Matlab?


回答1:


The simplest solution would be computing the norms first using element-wise multiplication and summation along the desired dimensions:

normA = sqrt(sum(A .^ 2, 2));
normB = sqrt(sum(B .^ 2, 1));

normA and normB are now a column vector and row vector, respectively. To divide corresponding elements in A * B by normA and normB, use bsxfun like so:

C = bsxfun(@rdivide, bsxfun(@rdivide, A * B, normA), normB);


来源:https://stackoverflow.com/questions/14340275/how-to-compute-cosine-similarity-using-two-matrices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!