0
votes

I need to compare the elements of two matrices and return a count of how many rows are exactly same. The ismember function returns one column for each column present in the matrix. But I want just one column indicating whether the row was same or not. Any ideas will be greatly appreciated.

1
Do you mean compare i-th with i-th row, or i-th row with j-th row for all pairs of i, j?Luis Mendo
@LuisMendo: Yes. Compare i-th row with i-th row. Both the matrices have same number of row.user3527975
See my answer then, first partLuis Mendo

1 Answers

1
votes

If you want to compare corresponding rows of the two matrices, just use

result = all(A==B, 2);

Example:

>> A = [1 2; 3 4; 5 6]
A =
     1     2
     3     4
     5     6
>> B = [1 2; 3 0; 5 6]
B =
     1     2
     3     0
     5     6
>> result = all(A==B, 2)
result =
     1
     0
     1

If you want to compare all pairs of rows:

result = pdist2(A,B)==0;

Example:

>> A = [1 2; 3 4; 1 2]
A =
     1     2
     3     4
     1     2
>> B = [1 2; 3 0]
B =
     1     2
     3     0
>> result = pdist2(A,B)==0
result =
     1     0
     0     0
     1     0