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:
Vectorization question (newbie alert)

Subject: Vectorization question (newbie alert)

From: Richard

Date: 17 May, 2013 21:59:08

Message: 1 of 3

Until yesterday, I've never used MATLAB (however I've done a fair amount of programming in languages like C/C++, java, etc.). I'm trying to assist someone with optimizing one of their modules that takes many hours to run. Using the profiler, I found that one loop was taking the lion's share of the processing time. I've just come across the "vectorization" feature that is supposed to help make loops unnecessary. When I employ it to remove the offending loop (hopefully correctly), it cuts the processing time down by a whopping 87%! I just need someone to verify that I'm using vectorization correctly. Here is the code snippet from the module that I did the vectorization on:

Original code:

  for j = 1:20
    f = f + nn(i,j) * log(p(j));
  end

My attempt at vectorization:

  j = 1:20
  f = sum(nn(i,j) .* log(p(j)));

Yea? Nay?

Am I using the correct multiplication operator in my code?

The module runs an iterative process. Normally there are 500 iterations and takes almost 9 hours on my dual quad-core i7-2600 @ 3.4GHz. When I set the number of iterations to 2 and then run the module with the two above variations, the profiler shows me the run time using the first algorithm is 679s compared to 88s using the second algorithm. That's huge! Question is, is the vectorization used correctly? (The final answers I'm getting are the same, but as I only have one data set, I can't say that the same result is not based on some characteristic of the data set I have.)

Thank-you.

Subject: Vectorization question (newbie alert)

From: James Tursa

Date: 17 May, 2013 22:18:09

Message: 2 of 3

"Richard " <irecycle@shaw.ca> wrote in message <kn697c$7tp$1@newscl01ah.mathworks.com>...
> Until yesterday, I've never used MATLAB (however I've done a fair amount of programming in languages like C/C++, java, etc.). I'm trying to assist someone with optimizing one of their modules that takes many hours to run. Using the profiler, I found that one loop was taking the lion's share of the processing time. I've just come across the "vectorization" feature that is supposed to help make loops unnecessary. When I employ it to remove the offending loop (hopefully correctly), it cuts the processing time down by a whopping 87%! I just need someone to verify that I'm using vectorization correctly. Here is the code snippet from the module that I did the vectorization on:
>
> Original code:
>
> for j = 1:20
> f = f + nn(i,j) * log(p(j));
> end
>
> My attempt at vectorization:
>
> j = 1:20
> f = sum(nn(i,j) .* log(p(j)));
>
> Yea? Nay?
>
> Am I using the correct multiplication operator in my code?

Yes, assuming that the first loop above has a f = 0 statement prior to loop entry. The following does the same thing using matrix multiplication. It likely won't be any faster for you but I am just showing you another way:

   j = 1:20
   f = nn(i,j) * log(p(j)).'; % The matrix multiply essentially does the sum part

James Tursa

Subject: Vectorization question (newbie alert)

From: Richard

Date: 17 May, 2013 22:27:06

Message: 3 of 3

"James Tursa" wrote in message <kn6ab1$aqg$1@newscl01ah.mathworks.com>...
> "Richard " <irecycle@shaw.ca> wrote in message <kn697c$7tp$1@newscl01ah.mathworks.com>...
> > Until yesterday, I've never used MATLAB (however I've done a fair amount of programming in languages like C/C++, java, etc.). I'm trying to assist someone with optimizing one of their modules that takes many hours to run. Using the profiler, I found that one loop was
[snip]
> > Yea? Nay?
> >
> > Am I using the correct multiplication operator in my code?
>
> Yes, assuming that the first loop above has a f = 0 statement prior to loop entry. The following does the same thing using matrix multiplication. It likely won't be any faster for you but I am just showing you another way:
>
> j = 1:20
> f = nn(i,j) * log(p(j)).'; % The matrix multiply essentially does the sum part
>
> James Tursa

Thanks much for your response, James. Much appreciated.

I am amazed at the amount of time saved with vectorization. Clearly the person that originally coded the module wasn't aware of this.

I'm going to try your matrix multiplication version just for giggles!

Thanks again!

Richard

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