<?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>Programming in MATLAB-For Loops</name>
  <metadata>
  <md:version>1.3</md:version>
  <md:created>2006/01/20 23:25:51 US/Central</md:created>
  <md:revised>2006/01/26 15:50:55.617 US/Central</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>for loop</md:keyword>
    <md:keyword>M-File</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Programming</md:keyword>
  </md:keywordlist>

  <md:abstract>This is a tutorial on using For Loops in MATLAB.</md:abstract>
</metadata>
  <content>

<section id="SecForLoop">
	  <name>The For Loop</name>
<para id="ParaForLoopIntro">
The <emphasis>for loop</emphasis> is one way to make MATLAB repeat a series of computations using different values. The for loop has the following syntax:
<code type="block">
for d = array
    % MATLAB command 1
    % MATLAB command 2
    % and so on
end
</code>
In the for loop, <code>array</code> can be any vector or array of values.   The for loop works like this: <code>d</code> is set to the first value in <code>array</code>, and the sequence of MATLAB commands in the body of the for loop is executed with this value of <code>d</code>. Then <code>d</code> is set to the second value in  <code>array</code>, and the sequence of MATLAB commands in the body of the for loop is executed with this value of <code>d</code>. This process continues through all of the values in <code>array</code>.
So a for loop that performs computations for values of <code>d</code> from 1.0 to 2.0 is:
<code type="block">
for d = 1.0:0.05:2.0
    % MATLAB command 1
    % MATLAB command 2
    % and so on
end
</code>
(Recall that <code>1.0:0.05:2.0</code> creates a vector of values from 1.0 to 2.0.)
</para>
<para id="element-194">Note that in all of the examples in this module, the MATLAB commands inside the for loop are indented relative to the <code>for</code> and <code>end</code> statements. This is not required by MATLAB but is common practice and makes the code much more readable.</para><para id="ParaForLoopIntro2">
A useful type of for loop is one that steps a counter variable from 1 to some upper value:
<code type="block">
for j = 1:10
    % MATLAB commands
end
</code>
For example, this type of loop can be used to compute a sequence of values that are stored in the elements of a vector. An example of this type of loop is
<code type="block">
% Store the results of this loop computation in the vector v
for j = 1:10
    % MATLAB commands
    % More MATLAB commands to compute a complicated result
    v(j) = result;
end
</code>
</para><para id="element-199">Using a for loop to access and manipulate elements of a vector (as in this example) may be the most natural approach, particularly when one has previous experience with other programming languages such as C or Java. However, many problems can be solved without for loops by using MATLAB's built-in vector capabilities. Using these capabilities almost always improves computational speed and reduces the size of the program. Some would also claim that it is more elegant.</para><para id="element-310">For loops can also contain other for loops. For example, the following code performs the MATLAB commands for each combination of <code>d</code> and <code>c</code>:
<code type="block">
for d=1:0.05:2
    for c=5:0.1:6
        % MATLAB Commands
    end
end
</code></para>

</section>

  </content>
</document>
