Open In App

Cosine Similarity Calculation Between Two Matrices in MATLAB

MATLAB (Matrix Laboratory) is a high-level programming language and numerical computing environment for performing complex mathematical computations and simulations. It is used in a wide range of applications including signal and image processing, control systems, and engineering and scientific calculation.

Cosine Similarity

Cosine Similarity is a measure of similarity between two non-zero vectors of an inner product space. It calculates the cosine of the angle between the two vectors and returns a value between -1 and 1, where 1 means the vectors are perfectly similar and -1 means they are perfectly dissimilar. Cosine similarity is often used in natural language processing and information retrieval to determine the similarity between documents or text.



Steps:

To calculate cosine similarity between two matrices in MATLAB, you can follow these steps:



Example 1: 




% Load or define matrices A and B
A = [1 2; 3 4];
B = [5 6; 7 8];
  
% Normalize the matrices
A = A ./ norm(A,2);
B = B ./ norm(B,2);
  
% Calculate dot product 
% between each row of the matrices
dot_product = A * B';
  
% Calculate cosine similarity
% between each row
cosine_similarity = dot_product ./ (norm(A,2) .* norm(B,2)')
  
% Average cosine similarity 
% between all rows (optional)
mean_cosine_similarity = mean(mean(cosine_similarity))

Output:

Output

Example 2:




% Load or define matrices A and B
A = [1 2; 3 4];
B = [5 6; 7 8];
  
% Normalize the matrices
A = bsxfun(@rdivide, A, sqrt(sum(A.^2,2)));
B = bsxfun(@rdivide, B, sqrt(sum(B.^2,2)));
  
% Calculate dot product between
% each row of the matrices
dot_product = A * B';
  
% Calculate cosine similarity 
% between each row
cosine_similarity = dot_product;
  
% Average cosine similarity 
% between all rows (optional)
mean_cosine_similarity = mean(mean(cosine_similarity));

Output:

Output image

Code explanation:

  1. The first two lines define the two matrices A and B that you want to compare.
  2. The next two lines normalize the matrices to ensure that they have unit length. The bsxfun function is used to divide each row of the matrices by the square root of the sum of squares of the elements in that row. This step is necessary because cosine similarity is based on the angle between vectors and the cosine of the angle between two vectors is equal to their dot product divided by the product of their magnitudes.
  3. The fifth line calculates the dot product between each row of the two matrices. The result is a matrix where each element is the dot product between a pair of rows from A and B.
  4. The sixth line calculates the cosine similarity between each row. Since the matrices have already been normalized, the dot product between each pair of rows is equal to the cosine of the angle between them and can be used directly as the cosine similarity.
  5. The final line calculates the average cosine similarity between all rows if desired. This is done by taking the mean of the cosine_similarity matrix.

Note: In the code, the normalization step is done using bsxfun, which is a built-in MATLAB function for element-wise operations. An alternative method for normalization would be to use a for-loop to iterate over each row of the matrices and normalize each row individually.

Advantages of Cosine Similarity Between Two Matrices:

Disadvantages of Cosine Similarity Between Two Matrices:


Article Tags :