# How to find percentage of Similarity between two logical matrices

5 Ansichten (letzte 30 Tage)
julian gaviria am 25 Jan. 2023
Kommentiert: julian gaviria am 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);
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
the cyclist am 25 Jan. 2023
Bearbeitet: the cyclist am 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?

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Image Analyst am 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?
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
julian gaviria am 25 Jan. 2023
Thanks a lot @Image Analyst and @the cyclist for replying.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by