for i=1:cols(M)
M(:,i) = M(:,i)*v(i)
end
Is there a fast/vectorized way to do this? Is this faster:
M = M*repmat(v,rows(M),1);
Thank you
Misha
That does not work.
If
A = R*M* C
where R and C are diagonal, then
a(i,j) = r(i)*m(i,j)*c(j)
i.e., r(i) multiplies each element in the ith row and c(j)
multiplies each element in the jth column. So, if r and c
are vectors,
A = diag(r)*M*diag(c)
Hope this helps.
Greg
% Engine
D1 = bsxfun(@times,M,v);
% Compare to other method
for i=1:size(M,2)
D2(:,i) = M(:,i)*v(i);
end
all(D1(:)==D2(:))
In general, use BSXFUN; if M is square (NxN) you can also use
M * diag(v)
hth
Jos
> if M is square (NxN) you can also use
>
> M * diag(v)
>
This works even for M is (M x N) rectangle, the only requirement is v has N elements.
Bruno
Yep! Thanks for correcting me, Bruno.