<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451</link>
    <title>MATLAB Central Newsreader - Problem With a Simple For Loop</title>
    <description>Feed for thread: Problem With a Simple For Loop</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 18 Oct 2009 18:58:02 -0400</pubDate>
      <title>Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#687909</link>
      <author>Ali Moradi</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:&lt;br&gt;
&lt;br&gt;
for i = 2:9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dzu(i) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dzl(i) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dx(i) = PPX(i)-PPX(i-1);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. Does anyone have any idea as to why this is happening? Thank you all in advance for your help.&lt;br&gt;
&lt;br&gt;
- Ali</description>
    </item>
    <item>
      <pubDate>Sun, 18 Oct 2009 19:21:00 -0400</pubDate>
      <title>Re: Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#687914</link>
      <author>Bruno Luong</author>
      <description>&quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &amp;lt;hbfofq$9mi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 2:9&lt;br&gt;
&amp;gt;     dzu(i) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;gt;     dzl(i) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;gt;     dx(i) = PPX(i)-PPX(i-1);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. &lt;br&gt;
&lt;br&gt;
No, length 9 is correct.&lt;br&gt;
&lt;br&gt;
&amp;gt; Does anyone have any idea as to why this is happening? &lt;br&gt;
&lt;br&gt;
A simply setting&lt;br&gt;
&lt;br&gt;
d(1000)=1&lt;br&gt;
&lt;br&gt;
will give 1x1000 vector&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 18 Oct 2009 19:53:01 -0400</pubDate>
      <title>Re: Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#687920</link>
      <author>Ali Moradi</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;hbfpqs$4bp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &amp;lt;hbfofq$9mi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for i = 2:9&lt;br&gt;
&amp;gt; &amp;gt;     dzu(i) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;gt; &amp;gt;     dzl(i) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;gt; &amp;gt;     dx(i) = PPX(i)-PPX(i-1);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; No, length 9 is correct.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Does anyone have any idea as to why this is happening? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A simply setting&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; d(1000)=1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; will give 1x1000 vector&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I see what you mean, and I think I fixed it. I just added another loop and made it look like this:&lt;br&gt;
&lt;br&gt;
for j = 1:8&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i = 2:9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dzu(j) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dzl(j) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dx(j) = PPX(i)-PPX(i-1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Does that look about right? Thanks again.&lt;br&gt;
&lt;br&gt;
- Ali</description>
    </item>
    <item>
      <pubDate>Sun, 18 Oct 2009 20:01:04 -0400</pubDate>
      <title>Re: Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#687923</link>
      <author>Bruno Luong</author>
      <description>&quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; Does that look about right?&lt;br&gt;
&lt;br&gt;
No, sorry it's all wrong.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 19 Oct 2009 02:50:22 -0400</pubDate>
      <title>Re: Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#687971</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &lt;br&gt;
news:hbfrmt$mi$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;hbfpqs$4bp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; &quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt;&amp;gt; &amp;lt;hbfofq$9mi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; I see what you mean, and I think I fixed it. I just added another loop and &lt;br&gt;
&amp;gt; made it look like this:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for j = 1:8&lt;br&gt;
&amp;gt;    for i = 2:9&lt;br&gt;
&amp;gt;        dzu(j) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;gt;        dzl(j) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;gt;        dx(j) = PPX(i)-PPX(i-1);&lt;br&gt;
&amp;gt;    end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Does that look about right? Thanks again.&lt;br&gt;
&lt;br&gt;
There's no need for a double loop here.  Either use the loop from 2 to 9 and &lt;br&gt;
remove the first element after the loop is finished:&lt;br&gt;
&lt;br&gt;
dzu(1) = [];&lt;br&gt;
&lt;br&gt;
or find some way to 'map' 2:9 to 1:8 and use that 'map' to convert the loop &lt;br&gt;
variable into the appropriate index value.  If you understand the following &lt;br&gt;
example, you should be able to adapt it to your needs:&lt;br&gt;
&lt;br&gt;
z = zeros(1, 10);&lt;br&gt;
for k = 2:2:20&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;z(k/2) = k;&lt;br&gt;
end&lt;br&gt;
% Of course, if that was really your goal, you would just use z = 2:2:20;&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com&lt;br&gt;
comp.soft-sys.matlab (CSSM) FAQ: &lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt; </description>
    </item>
    <item>
      <pubDate>Mon, 19 Oct 2009 08:30:05 -0400</pubDate>
      <title>Re: Problem With a Simple For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263451#688014</link>
      <author>Steve Amphlett</author>
      <description>&quot;Ali Moradi&quot; &amp;lt;kimusubi@gmail.com&amp;gt; wrote in message &amp;lt;hbfofq$9mi$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 2:9&lt;br&gt;
&amp;gt;     dzu(i) = PPZU(i)-PPZU(i-1);&lt;br&gt;
&amp;gt;     dzl(i) = PPZL(i)-PPZL(i-1);&lt;br&gt;
&amp;gt;     dx(i) = PPX(i)-PPX(i-1);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. Does anyone have any idea as to why this is happening? Thank you all in advance for your help.&lt;br&gt;
&lt;br&gt;
Why loop at all?  diff() will do your homework for you.</description>
    </item>
  </channel>
</rss>

