# Cosine Similarity Calculation Between Two Matrices in MATLAB

In this tutorial, we will learn how to calculate cosine similarity between two matrices using MATLAB. So, let’s start with the basic definition of cosine similarity.

## What is Cosine Similarity?

Cosine similarity is a standard of measurement in matrix algebra used to determine the measure of similarity between two non-zero matrices in an inner product space like Euclidean space.

Cosine similarity measures the cosine of the angle between the two matrices and produces a value between -1 and 1. Here, the value 1 indicates that the two matrices are perfectly similar, and the value -1 indicates that the two matrices are perfectly dissimilar or pointing in opposite directions (diametrically opposite). Also, if the value of cosine similarity is 0, then it shows that the two matrices are perpendicular or orthogonal and having no similarity between them.

Therefore, we can say that cosine similarity is a way of determining how closely the two matrices are aligned with each other in terms of their orientation.

Cosine similarity is widely used in various fields of science and technology, such as digital image processing, natural language processing, control systems, data mining, recommendation systems, and more.

### Cosine Similarity Formula

Consider A and B are two non-zero matrices, then the cosine similarity between these two matrices can be calculated by using the following formula −

$$\mathrm{Cosine \: Similarity(𝐴, 𝐵) =\frac{Dot \: Product \: of \: A \: and \: B}{(Magnitude \: of\: A) \ast (Magnitude \: of \: B)}}$$

Here, the dot product of A and B is the sum of element wise multiplication of the matrices, and the magnitudes of A and B are the square root of sum of squares of their components.

The following are some important advantages of cosine similarity −

• Cosine similarity does not depend on magnitudes of the matrices, but depends only on angle between them.

• Cosine similarity provides directional information between matrices, which is helpful in knowing how closely they are aligned.

• Cosine similarity involves very simple mathematical calculations.

• Cosine similarity is a highly versatile technique, as it can be used in various applications like image processing, natural language processing, control systems, information retrieval, document clustering, and many more.

Apart from advantages, cosine similarity also has some disadvantages. Some key disadvantages of cosine similarity are listed below −

• Cosine similarity does not use the magnitude of matrices in calculations. Hence, this technique cannot be used in applications where magnitude information is crucial.

• Cosine similarity is highly sensitive to the angle between the matrices. Thus, a minor change in the angle can cause a significant error in the calculation.

• Cosine similarity does not use the semantic meaning of data in calculations and consider all dimensions of the matrices equally.

After getting a brief overview of cosine similarity, its formula, advantages, and disadvantages. Let us now discuss how to calculate cosine similarity between two matrices using MATLAB.

## Calculate Cosine Similarity Between Two Matrices Using MATLAB

In MATLAB, we can calculate the cosine similarity between two matrices as described in the following steps −

• Step (1) − Create two matrices.

• Step (2) − Normalize the matrices so they have unit length.

• Step (3) − Calculate the dot product of the two matrices.

• Step (4) − Calculate the magnitude of both matrices.

• Step (5) − Use the formula of cosine similarity to calculate the similarity value between the two matrices.

• Step (6) − Display the result.

Now, let us take some examples to understand how to implement MATLAB code to calculate cosine similarity between two matrices.

### MATLAB Example

% MATLAB program to demonstrate cosine similarity calculation
% Create two sample matrices
A = [1 3 5; 2 4 6; 7 9 2];
B = [7 9 8; 6 4 5; 3 1 2];

% Normalize the matrices A and B
Normalized_A = A ./ norm(A, 'fro');
Normalized_B = B ./ norm(B, 'fro');

% Calculate dot product between matrices A and B
Dot_Product = sum(sum(A .* B));

% Calculate the magnitudes of matrices A and B
Magnitude_A = sqrt(sum(sum(A.^2)));
Magnitude_B = sqrt(sum(sum(B.^2)));

% Calculate the cosine similarity between matrices A and B
Cosine_Similarity = Dot_Product ./ (Magnitude_A * Magnitude_B);

% Display the matrices A and B and their cosine similarity value
disp('Matrix A: ');
disp(A);
disp('Matrix B: ');
disp(B);
disp('Cosine Similarity between Matrices A and B: ');
disp(Cosine_Similarity);


### Output

Matrix A:
1     3     5
2     4     6
7     9     2

Matrix B:
7     9     8
6     4     5
3     1     2

Cosine Similarity between Matrices A and B:
0.6555


## Conclusion

In this tutorial, we explained what is cosine similarity and its applications. We discussed the step-by-step process to calculate the cosine similarity between two matrices, also included an example program to demonstrate the practical implementation of the cosine similarity calculation.

Updated on: 06-Sep-2023

113 Views 