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.