Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to avoid the for here?

Subject: How to avoid the for here?

From: Alfredo Rojas

Date: 1 Jun, 2010 21:57:03

Message: 1 of 2


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?

Subject: How to avoid the for here?

From: Walter Roberson

Date: 1 Jun, 2010 22:21:35

Message: 2 of 2

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);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us