 <p>You could also do it with <a href="http://es.mathworks.com/help/matlab/ref/bsxfun.html" rel="nofollow"><code>bsxfun</code></a>:</p> <pre><code>result = sum(bsxfun(@eq, a(:).', b(:)), 1); </code></pre> <p>This has the advantage that it tells you <em>how many</em> elements of <code>b</code> equal each element of <code>a</code>. For example,</p> <pre><code>&gt;&gt; a = [1 2 3]; &gt;&gt; b = [2 3 4 2]; &gt;&gt; result = sum(bsxfun(@eq, a(:).', b(:)),1) result = 0 2 1 </code></pre>