Say I have two very big matrices A (M-by-N) and B (N-by-M). I need the diagonal of A*B. Computing the full A*B requires M
A
B
A*B
You can compute only the diagonal elements without a loop: just use
sum(A.'.*B).'
or
sum(A.*B.',2)