@ Walter & @ Bruno: both of your answers are slower than the For loop, especially when I used larger matrics, any idea? I need a solution that is faster than For loop. Please see validation below:
tic
a = rand(1000,1000);
b = rand(500,1000,1000);
for i = 1:500
c = reshape(b(i, :, :),1000,1000);
d(i) = sum(sum(a.*c));
end
toc
Elapsed time is 27.988668 seconds.
%%%%%%%%
% Walter answer:
tic
d = sum(b .* reshape(a, 1, 1000, 1000), [2 3]);
toc
Elapsed time is 68.882801 seconds.
%%%%%%%%
% Bruno answer:
tic
d = b(:,:)*a(:);
toc
Elapsed time is 46.952318 seconds.