Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. With the help of this software, we can find how many elements are the same in two matrices and also calculate the percentage of similarity between the two matrices.

Formula:

Percentage of Similarity = (Number of same elements)/(row*column) * 100

Steps:

  1. Read the matrices.
  2. Define a variable ‘equal’ storing the boolean result of matrix A == matrix B.
  3. Count the number of equal elements using the built-in sum function.
  4. Calculate and display the similarity percentage using the formula.

Example 1:

Matlab




% MATLAB code for dimensions of matrix
row = 3;
col = 3;
  
% Matrix 
A = [[2,3,5];[4,6,7];[8,1,9]];
B = [[2,10,5];[4,6,45];[8,78,9]];
  
% Counting equal elements
equal = A==B;
  
% Number of equal elements
equalCount = sum(equal(:));
similarityPercentage= (equalCount / (row * col) ) * 100;
  
% Display similarityPercentage
disp(similarityPercentage);


Output:

 

Example 2:

Matlab




% MATLAB code for dimensions of matrix
row = 3;
col = 2;
  
% Matrix 
A = [[7,9];[4,2];[5,1]];
B = [[3,12];[4,6];[8,1]];
  
% Counting equal elements
equal = A==B;
  
% Number of equal elements
equalCount = sum(equal(:));
similarityPercentage= (equalCount / (row * col) ) * 100;
  
% Display similarityPercentage
disp(similarityPercentage);


Output:

 

Matlab Code for Example 3

Matlab




% MATLAB code for dimensions of matrix
row = 3;
col = 4;
  
% Matrix 
A = [[1,5,9,4];[3,2,6,7];[8,12,11,10]];
B = [[1,2,3,5];[9,8,7,6];[10,11,12,4]];
  
% Counting equal elements
equal = A==B;
  
% Number of equal elements
equalCount = sum(equal(:));
similarityPercentage= (equalCount / (row * col) ) * 100;
  
% Display similarityPercentage
disp(similarityPercentage);


Output:

 



Last Updated : 28 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads