How to Find Percentage of Similarity between Two Matrices in MATLAB?


As we know, MATLAB is a powerful tool that can be used to perform various operations on matrices. One such operation is finding the percentage of similarity between two matrices. Finding percentage of similarity between two matrices is crucial in various application such as digital image processing, data analysis, machine learning, and more.

This tutorial will help you understand the step-by-step process of finding the percentage of similarity between two matrices using MATLAB.

What is Percentage of Similarity Between Two Matrices?

The measure that shows how closely two matrices are related in some aspect is called "similarity between two matrices". The percentage of similarity between two matrices is nothing but the measure of how similar they are in terms of their elements.

The following mathematical formula can be used to calculate the percentage of similarity between two matrices.

$$\mathrm{\% \:of \:Similarity =\frac{Number \:of \:same \:elements}{Total \:number \:of \:elements}× 100}$$

It can also be written as,

$$\mathrm{\% \:of \:Similarity =\frac{Number \:of \:same \:elements}{Rows × Columns}× 100}$$

The percentage of similarity is used to quantify the measure of closeness between two matrices.

Let us now understand the step-by-step process to find the percentage of similarity between two matrices.

Find Percentage of Similarity Between Two Matrices in MATLAB

The step-by-step process to find the percentage of similarity between two matrices using MATLAB is explained here.

  • Step (1) − Create two matrices of same sizes between which you want to find the similarity.

  • Step (2) − Use the "==" operator to find the equality between two matrices. Store the resulting Boolean values in a variable.

  • Step (3) − Calculate the percentage of similarity between two matrices using the formula given above.

MATLAB provides a simple three step process to find the percentage of similarity between two matrices.

Example

Let us take some examples to understand how to calculate the percentage of similarity between two matrices in MATLAB.

% MATLAB program to find percentage of similarity between two square matrices
% Create two sample square matrices
A = [1 2 3 4; 1 4 2 3; 4 5 6 8; 2 3 7 6];
B = [1 4 3 2; 4 2 1 3; 1 5 7 8; 2 1 7 3];

% Specify the size of matrices
rows = 4;
cols = 4;

% Find the equality between matrices
same_elements = A == B;

% Count the number same elements
count_same_elements = sum(same_elements(:));

% Compute the percentage of similarity between two matrices
per_sim = count_same_elements / (rows * cols) * 100;

% Display the original matrices and percentage of similarity between them
disp('Matrix A is:');
disp(A);
disp('Matrix B is:');
disp(B);
disp('Percentage of Similarity between Matrices A and B is:');
disp(per_sim);

Output

When you run this code, it will produce the following output

Matrix A is:
     1     2     3     4
     1     4     2     3
     4     5     6     8
     2     3     7     6

Matrix B is:
     1     4     3     2
     4     2     1     3
     1     5     7     8
     2     1     7     3

Percentage of Similarity between Matrices A and B is:
   43.7500

Code Explanation

In this example, we start by declaring two matrices having some elements are same. Next, we specify the sizes of the two matrices. It is important that the size of the two matrices must be same for finding similarity between them.

Then, we count the number same elements in the two matrices using the "==" operator and store the Boolean result in a variable "same_elements", this variable is a matrix having each element "1" when the elements of the matrices "A" and "B" are same and "0" for unequal elements.

After that, we use the "sum" function to count the number of "1" in the matrix "same_elements". This is basically the number of same elements in the matrices A and B.

Then, we use the formula to find the percentage of similarity between two matrices.

Finally, we use the "disp" function to display the input matrices and the percentage of similarity between matrices A and B.

Example

Let us consider another example to calculate the percentage of similarity between two rectangular matrices.

% MATLAB program to find percentage of similarity between two rectangular matrices
% Create two sample rectangular matrices
A = [1 2 3 4; 1 4 2 3; 4 5 6 8];
B = [1 4 3 2; 4 2 1 3; 1 5 7 8];

% Specify the size of matrices
rows = 3;
cols = 4;

% Find the equality between matrices
same_elements = A == B;

% Count the number same elements
count_same_elements = sum(same_elements(:));

% Compute the percentage of similarity between two matrices
per_sim = count_same_elements / (rows * cols) * 100;

% Display the original matrices and percentage of similarity between them
disp('Matrix A is:');
disp(A);
disp('Matrix B is:');
disp(B);
disp('Percentage of Similarity between Matrices A and B is:');
disp(per_sim);

Output

When you run this code, it will produce the following output

Matrix A is:
     1     2     3     4
     1     4     2     3
     4     5     6     8

Matrix B is:
     1     4     3     2
     4     2     1     3
     1     5     7     8

Percentage of Similarity between Matrices A and B is:
   41.6667

Code Explanation

The implementation and execution of this MATLAB code is same as that of the previous one. The only difference is that in this code, we have taken two rectangular matrices instead of square matrices.

Conclusion

This is all about finding percentage of similarity between two matrices using MATLAB. In this tutorial, I explained the step-by-step process and examples to demonstrate the process of calculating the percentage of similarity between two matrices in MATLAB. You can try the above codes with your own matrices.

Updated on: 05-Oct-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements