user3527975
user3527975

Reputation: 1773

Find the count of elements in one matrix equal to another

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.

Upvotes: 0

Views: 275

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112769

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

Upvotes: 1

Related Questions