<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Vectorizing loops in MATLAB</name>
  <metadata>
  <md:version>1.4</md:version>
  <md:created>2006/01/20 03:05:06 US/Central</md:created>
  <md:revised>2006/07/21 02:25:03.542 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="Anders">
      <md:firstname>Anders</md:firstname>
      
      <md:surname>Gjendemsjø</md:surname>
      <md:email>gjendems@iet.ntnu.no</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="Anders">
      <md:firstname>Anders</md:firstname>
      
      <md:surname>Gjendemsjø</md:surname>
      <md:email>gjendems@iet.ntnu.no</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Loops</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Performance</md:keyword>
    <md:keyword>Vectorizing</md:keyword>
  </md:keywordlist>

  <md:abstract>In MATLAB, one should always try to avoid using loops. This module introduce the concept of vectorization.</md:abstract>
</metadata>
<content>
<section id="id31651512">
<!--<name>Name</name>-->
<para id="id31651519">In MATLAB one should try to avoid loops. This can be done by vectorizing your code. The idea is that MATLAB is very fast
on vector and matrix operations and correspondingly slow with loops. We illustrate this by an example.</para>

<example id="example1">
	<para id="element-480">Given
	<m:math>
		<m:apply>
			<m:eq/>
			<m:ci><m:msub><m:mi>a</m:mi><m:mi>n</m:mi></m:msub></m:ci>
			<m:ci>n</m:ci>
		</m:apply>
	</m:math>,
	and
	<m:math>
		<m:apply>
			<m:eq/>
			<m:ci><m:msub><m:mi>b</m:mi><m:mi>n</m:mi></m:msub></m:ci>
			<m:apply>
				<m:minus/>
				<m:cn>1000</m:cn>
				<m:ci>n</m:ci>
			</m:apply>
		</m:apply>
	</m:math>
	for 
	<m:math>
		<m:apply>
			<m:eq/>
			<m:ci>n</m:ci>
			<m:cn>1,...,1000</m:cn>
		</m:apply>
	</m:math>.
	Calculate
	<m:math>
		<m:apply>
				<m:sum/>
				<m:bvar><m:ci>n</m:ci></m:bvar>
				<m:lowlimit><m:cn>1</m:cn></m:lowlimit>
				<m:uplimit><m:cn>1000</m:cn></m:uplimit>
				<m:apply>
			  	<m:times/>
					<m:ci><m:msub><m:mi>a</m:mi><m:mi>n</m:mi></m:msub></m:ci>
					<m:ci><m:msub><m:mi>b</m:mi><m:mi>n</m:mi></m:msub></m:ci> 
				</m:apply>
	   </m:apply>
	 </m:math>,
	 and store in the variable <code>ssum</code>.</para>
	 <para id="exsol1">Solution:
	 It might be tempting to implement the above calculation as
<code type="block">
a = 1:1000;
b = 1000 - a;
ssum=0;
for n=1:1000 %poor style...
   ssum = ssum +a(n)*b(n);
end
</code>
Recognizing that the sum is the inner product of the vectors <m:math><m:ci>a</m:ci></m:math> and <m:math><m:ci>b</m:ci></m:math>,
<m:math>
	<m:apply>
		<m:times/>
		<m:ci>a</m:ci>
		<m:apply>
			<m:power/>
			<m:ci>b</m:ci>
			<m:ci>T</m:ci>
		</m:apply>
	</m:apply>
</m:math>, we can do better:
<code type="block">
ssum = a*b' %Vectorized, better!
</code>
</para></example>
<para id="finalNote">
For more detailed information on vectorization, please take a look at MathWorks'
<link src="http://www.mathworks.com/support/tech-notes/1100/1109.html">Code Vectorization Guide</link>.
</para>


</section>
</content>
  
</document>
