If you write it all out for two 3×3 matrices, you'll find that the operation basically equals this:
C = bsxfun(@times, sum(B), sum(A).');
Running each of the answers here for N=50
, G=100
and repeating each method 100 times:
Elapsed time is 13.839893 seconds. %// OP's original method
Elapsed time is 19.773445 seconds. %// Luis' method
Elapsed time is 0.306447 seconds. %// Robert's method
Elapsed time is 0.005036 seconds. %// Rody's method
(a factor of ≈ 4000 between the fastest and slowest method...)