Monday, May 11, 2009

Matlab: Vector operation

OP here.

Q:

Is there a vectorized way for this code?

for i=1:size(m,2)
m(:,i) = m(:,i).*v(i);
end

A1:

mv = bsxfun(@times,m,v');


A2:

mv = m*diag(v);


all these solutions are much better than using repmat etc.

No comments: