The simplest solution would be computing the norms first using element-wise multiplication and summation along the desired dimensions:
normA = sqrt(sum(A .^ 2, 2));
normB = sqrt(sum(B .^ 2, 1));
normA
and normB
are now a column vector and row vector, respectively. To divide corresponding elements in A * B
by normA
and normB
, use bsxfun
like so:
C = bsxfun(@rdivide, bsxfun(@rdivide, A * B, normA), normB);