Matches rows in a matrix

I have a simulation code that creates a binary matrix in matlab always with 10 rows, but with a different number of columns.

For instance:

 1     0     0     0
 0     0     0     0
 0     1     0     0
 1     0     0     0
 1     0     0     0
 1     0     1     0
 0     0     0     1
 1     0     0     0
 0     0     0     0
 0     0     0     0

I want to do a pairwise comparison between the lines to determine how many elements differ between the two lines, eventually create a 10x10 symmetric matrix with the number of differences between the lines. For instance. Line 1 compared to line 2 ... and so on.

Thus, the element (1,2) (and also the element 2,1) of this matrix will compare row 1 with row 2 and in this case will be 1, since there is only one difference.

I know this can be done with a lot of coding of the loop, however, there seems to be a simpler way that I don't know about.

How should this be achieved?

+5
1

- :

A=[1 0 1; 0 1 1; 1 1 1 ; 0 0 0];
A*(1-A)'+(1-A)*A'
ans =
 0     2     1     2
 2     0     1     2
 1     1     0     3
 2     2     3     0

, a b iff a = 1 b = 0 = 0 b = 1, a * (1-b) + b * (1-a).

(i, j). , .

+4

All Articles