From: Alfredo Rojas on

Hello, I have to multiply each row of a matrix by a scalar contained in a vector, it's somthing like this

a=rand(3,7);
b=(1:7);
for i=1:7
a(:,i)=a(:,i).*b(i)
end

I have to do something like this but with very large data in a simulation so the "for" makes it last so long, so, How avoid it?
From: Walter Roberson on
Alfredo Rojas wrote:
>
> Hello, I have to multiply each row of a matrix by a scalar contained in
> a vector, it's somthing like this
>
> a=rand(3,7);
> b=(1:7);
> for i=1:7
> a(:,i)=a(:,i).*b(i)
> end
>
> I have to do something like this but with very large data in a
> simulation so the "for" makes it last so long, so, How avoid it?

I don't know if you will be able to get faster, but you could use

a = bsxfun(@times, a, b);