# How to find percentage of Similarity between two logical matrices

10 views (last 30 days)
julian gaviria on 25 Jan 2023
Commented: julian gaviria on 25 Jan 2023
The following code calculates the percentage of similarity between the attached logical matrices "c1" and "c2":
row = 168;
col = 390;
% Counting equal elements
equal = c1==c2;
% Number of equal elements
equalCount = sum(equal(:));
similarityPercentage= (equalCount / (row * col) ) * 100;
% Display similarityPercentage
disp(similarityPercentage);
How can I compare the same percentage of similarity only considering the matrix places with "1" values. To note, it's not about counting the number of ones. The aim is to see in which percentage the "1" values appear in both metrices in terms of their number (i.e., quantity) and their position in the respective matrices (i.e., row and col).
P.D., I also calculate the mse values for cross validation.
mse_val=mse(c1,c2);
the cyclist on 25 Jan 2023
Edited: the cyclist on 25 Jan 2023
FYI, you can do your original calculation with this one-liner:
similarityPercentage = mean(c1==c2,"all")*100
I don't completely understand your question, though. Maybe you could use a smaller example, like
c1 = [1 1 1;
0 1 1;
0 0 1];
c2 = [1 0 0;
1 1 0;
1 1 0];
and tell us exactly what you expect the output to be?

Image Analyst on 25 Jan 2023
Wouldn't it just be
row = 168;
col = 390;
% Counting equal elements
equal = c1==c2;
% Number of equal elements
equalCount = sum(equal(:));
% Count the number of 1's total
oneCount = sum(c1 | c2, 'all')
% Compute the percentage of both having 1 as a fraction of either having 1.
similarityPercentage = (equalCount / oneCount) * 100;
% Display similarityPercentage
disp(similarityPercentage);
or am I missing something?
julian gaviria on 25 Jan 2023
Thanks a lot @Image Analyst and @the cyclist for replying.

### Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

R2022b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by