MATLAB: How to vectorize this code

vectorization

Input:
  • A: (m x n)
  • B: (k x l) with k,l > m,n respectively
Output:
  • C: (p x q) with p=k-m +1 and q=l-n+1
Each element of C is the sum of the "element by element" product of A and a (m x n) submatrix of B.
  • For C(1,1) the (m x n) submatrix is located in the bottom right corner of B.
  • For C(u<p,v<q) the (m x n) submatrix is shifted by u upwards and v leftwards.
  • For C(p,q) the (m x n) submatrix is located in the top left corner of B.
My code:
C = zeros(p,q);
for u = 1:1:p
for v = 1:1:q
C(u,v) = sum(sum(A .* B( p-u+1:k-u+1 , q-v+1:l-v+1 )));
end
end
It works fine but is way too slow (A and B are very large and contain complex values).
Question:
How can I vectorize this code to increase the speed ?

Best Answer

C = rot90(filter2(A,B,'valid'),2);
or
C = conv2(rot90(B,2),A,'valid');