Question

I use c=bsxfun(@eq,b,a) to compare value of two matrix. but I find it difficult to count un-match values. for example, I use this code

a = [1 2 3 4 7 6; ...
     3 2 4 6 7 2 ];
b = [1 3 2 4 5 7; ...
     3 4 5 6 7 2; ...
     2 3 4 5 6 6];
for i = 1:size(a,1)
    c= bsxfun(@eq,a(i,:),b)
    match = sum(c')
end

and result

 c =
   1     0     0     1     0     0
   0     0     0     0     1     0
   0     0     0     0     0     1

 match =
   2     1     1

c =
   0     0     0     0     0     0
   1     0     0     1     1     1
   0     0     1     0     0     0

match =
   0     4     1

I want to save value first match matrix with second match. for example

total_match = 
   2 5 2

Do you have any suggestion ? thanks..

Was it helpful?

Solution

No need for loop

match = bsxfun( @eq, permute( a, [1 3 2]), permute( b, [3 1 2] ) ); % result in 2x3x6 boolean
match = sum( match, 3 ); % sum matches across rows of a--b
total_match = sum( match, 1 ); 

PS
It is best not to use i and j as variable names in Matlab.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top