MATLAB: Can someone do this calculation without for loops

matrix manipulation

a = [1 2 3; 4 5 6];
b = [ 1 2 3];
for n = 1: size(a,1)
for m = 1:size(a,2)
k(n,m,:)= b.*b*a(n,m)
end
end

Best Answer

k = bsxfun(@times, a , reshape(b.^2,1,1,[]))
alt_k = bsxfun(@times, a , permute(b.^2,[3,1,2]))
Related Question