 <p>Correct...summing the element-wise products will be quicker:</p> <pre><code>n = 1000 A = randn(n); B = randn(n); tic sum(sum(A .* B)); toc tic sum(diag(A * B')); toc </code></pre> <pre class="lang-or-tag-here prettyprint-override"><code>Elapsed time is 0.010015 seconds. Elapsed time is 0.130514 seconds. </code></pre>