<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How for Works</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/07/19/how-for-works/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-32839</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Tue, 13 Dec 2011 15:50:55 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-32839</guid>
		<description>Naama,

You can create a secondary counter that you increment or not at the relevant places and make your decision based on that derived value.  You might also consider the break or continue commands in MATLAB for more control over the loop.

--Loren</description>
		<content:encoded><![CDATA[<p>Naama,</p>
<p>You can create a secondary counter that you increment or not at the relevant places and make your decision based on that derived value.  You might also consider the break or continue commands in MATLAB for more control over the loop.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naama</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-32838</link>
		<dc:creator>Naama</dc:creator>
		<pubDate>Tue, 13 Dec 2011 15:44:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-32838</guid>
		<description>Hi Loren,

I have a question regarding the scope of for. You gave an example where you try to change the for loop variable:

for ind = 1:3
    ind
    ind = 10
end
ind

and yet the for loop continues as if you haven&#039;t. Is there away past this? I am checking something with an &quot;if&quot; inside the loop, and in some cases I would like to make the loop run one more, so I would like to do something like this: ind=ind-1
but I would like this to have the desired effect of making to loop run with the same ind value again.

Thanks for your help!
Naama</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I have a question regarding the scope of for. You gave an example where you try to change the for loop variable:</p>
<p>for ind = 1:3<br />
    ind<br />
    ind = 10<br />
end<br />
ind</p>
<p>and yet the for loop continues as if you haven&#8217;t. Is there away past this? I am checking something with an &#8220;if&#8221; inside the loop, and in some cases I would like to make the loop run one more, so I would like to do something like this: ind=ind-1<br />
but I would like this to have the desired effect of making to loop run with the same ind value again.</p>
<p>Thanks for your help!<br />
Naama</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nolan Hergert</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-30398</link>
		<dc:creator>Nolan Hergert</dc:creator>
		<pubDate>Wed, 17 Jun 2009 17:07:05 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-30398</guid>
		<description>Nevermind! You can do it with a while loop and then a count variable that you increment (or decrement) based on your preferences.

&lt;pre&gt;
count = 1;
while (endloop == 0)
if (goforward == 1) 
count = count + 1
end
if (backup == 1) 
count = count - 1
end
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Nevermind! You can do it with a while loop and then a count variable that you increment (or decrement) based on your preferences.</p>
<pre>
count = 1;
while (endloop == 0)
if (goforward == 1)
count = count + 1
end
if (backup == 1)
count = count - 1
end
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-30397</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 17 Jun 2009 16:55:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-30397</guid>
		<description>Nolan-

I can&#039;t think of a way to do what you ask with a for loop.  You could more easily do it with a while loop however, since in that case, you can adjust the control parameter for the while loop inside the loop.  It also depends on whether or not the calculations later in the loop depend on previous iterations.  If they do not, then you might be able to vectorize the calculation, avoid the loop totally, then check the conditions afterwards and just recalculate the few elements that need to be adjusted.

--Loren</description>
		<content:encoded><![CDATA[<p>Nolan-</p>
<p>I can&#8217;t think of a way to do what you ask with a for loop.  You could more easily do it with a while loop however, since in that case, you can adjust the control parameter for the while loop inside the loop.  It also depends on whether or not the calculations later in the loop depend on previous iterations.  If they do not, then you might be able to vectorize the calculation, avoid the loop totally, then check the conditions afterwards and just recalculate the few elements that need to be adjusted.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nolan Hergert</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-30396</link>
		<dc:creator>Nolan Hergert</dc:creator>
		<pubDate>Wed, 17 Jun 2009 16:47:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-30396</guid>
		<description>Thank you for the informative help! 

To extend this, how would you &quot;redo&quot; an iteration in a for loop? I have a particularly long for loop repeatedly asking questions to my user, and I want to be able to redo a number if necessary in the middle of the loop.

Ex.
&lt;pre&gt;
for i = 1:100
%When i = 50, I realize that I want to do 49 again (in the middle of the loop)
end
&lt;/pre&gt;

Any ideas? If there&#039;s a way that I can do it without using for loops, that is also welcomed.</description>
		<content:encoded><![CDATA[<p>Thank you for the informative help! </p>
<p>To extend this, how would you &#8220;redo&#8221; an iteration in a for loop? I have a particularly long for loop repeatedly asking questions to my user, and I want to be able to redo a number if necessary in the middle of the loop.</p>
<p>Ex.</p>
<pre>
for i = 1:100
%When i = 50, I realize that I want to do 49 again (in the middle of the loop)
end
</pre>
<p>Any ideas? If there&#8217;s a way that I can do it without using for loops, that is also welcomed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cris Luengo</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-30285</link>
		<dc:creator>Cris Luengo</dc:creator>
		<pubDate>Wed, 06 May 2009 11:45:45 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-30285</guid>
		<description>I looked into this a little, and it seems to me that the for loop uses a signed integer as the index. This is why it couldn&#039;t go past 2.15e19. In the current version of MATLAB, the limit for the loop is 2^63 (they&#039;ve updated to 64-bit integers). It actually gives a warning when the loop index passes past this value (after interrupting with Ctrl-C):

Warning: FOR loop index is too large. Truncating to 9223372036854775807. 

This totally makes sense, of course. Someone needs to count to keep track of how many iterations have been done. &quot;for ii=1:inf&quot; is not the same as &quot;while 1&quot;!</description>
		<content:encoded><![CDATA[<p>I looked into this a little, and it seems to me that the for loop uses a signed integer as the index. This is why it couldn&#8217;t go past 2.15e19. In the current version of MATLAB, the limit for the loop is 2^63 (they&#8217;ve updated to 64-bit integers). It actually gives a warning when the loop index passes past this value (after interrupting with Ctrl-C):</p>
<p>Warning: FOR loop index is too large. Truncating to 9223372036854775807. </p>
<p>This totally makes sense, of course. Someone needs to count to keep track of how many iterations have been done. &#8220;for ii=1:inf&#8221; is not the same as &#8220;while 1&#8243;!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Neuberger</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-25402</link>
		<dc:creator>Daniel Neuberger</dc:creator>
		<pubDate>Sat, 26 Jan 2008 23:23:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-25402</guid>
		<description>Hmm, very interesting.  I think you&#039;re mostly right, but slightly off.  See Loren&#039;s post from last week: http://blogs.mathworks.com/loren/2006/07/12/what-are-you-really-measuring/, 
She explains the difference between 1:N and [1:N] in a for loop.  In short, 1:N is expanded as needed, while [1:N] is expanded at start so that MATLAB can complete the called for concatenate operation.  For example:
&lt;code&gt;
for x = [1:inf]
end
&lt;/code&gt;
will immediately produce an error because MATLAB can&#039;t store an infinite length array.  This:
&lt;code&gt;
for x = 1:inf
end
&lt;/code&gt;
works though because MATLAB expands it as needed.

However, apparently MATLAB actually does save the entire vector 1:inf somewhere despite the fact that all of the previous elements will never be used.  To verify this I ran:
&lt;code&gt;
[c, maxsize] = computer
&lt;/code&gt;
which revealed:

maxsize = 2.1475e+009

which is exactly where my for loops stops.

This seems to confirm our suspicions.  It also indicates that the for loop could be further optimized to throw out previous elements when given a vector that could be expanded as needed since there is no way to retrieve the previous elements anyway.</description>
		<content:encoded><![CDATA[<p>Hmm, very interesting.  I think you&#8217;re mostly right, but slightly off.  See Loren&#8217;s post from last week: <a href="http://blogs.mathworks.com/loren/2006/07/12/what-are-you-really-measuring/" rel="nofollow">http://blogs.mathworks.com/loren/2006/07/12/what-are-you-really-measuring/</a>,<br />
She explains the difference between 1:N and [1:N] in a for loop.  In short, 1:N is expanded as needed, while [1:N] is expanded at start so that MATLAB can complete the called for concatenate operation.  For example:<br />
<code><br />
for x = [1:inf]<br />
end<br />
</code><br />
will immediately produce an error because MATLAB can&#8217;t store an infinite length array.  This:<br />
<code><br />
for x = 1:inf<br />
end<br />
</code><br />
works though because MATLAB expands it as needed.</p>
<p>However, apparently MATLAB actually does save the entire vector 1:inf somewhere despite the fact that all of the previous elements will never be used.  To verify this I ran:<br />
<code><br />
[c, maxsize] = computer<br />
</code><br />
which revealed:</p>
<p>maxsize = 2.1475e+009</p>
<p>which is exactly where my for loops stops.</p>
<p>This seems to confirm our suspicions.  It also indicates that the for loop could be further optimized to throw out previous elements when given a vector that could be expanded as needed since there is no way to retrieve the previous elements anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Rossi</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-25385</link>
		<dc:creator>Jacob Rossi</dc:creator>
		<pubDate>Sat, 26 Jan 2008 05:06:38 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-25385</guid>
		<description>I get the same limit for FOR loops. While I&#039;m no expert, I hypothesize that the difference occurs between For and While because of variable declaration sizes. Consider these two loops:

for x=0:inf
end

x = 0
while 1
   x = x +1
end

The for loop terminates quite early with x equal to the 2.15e19 constant you speak of. However, the while loop can march on much past it.

My guesstimation is that MATLAB allocates all of the FOR loop values (the vector 1:inf) at once. The size of which reaches the MATLAB limit for number of elements in a vector. So I assume MATLAB, truncates the size of the vector to this limitation.

Meanwhile, the while loop only deals with a 1x1 double and will only error out when it reaches the maximum value for a double.

Further proof of this is demonstrated when you try to create a vector 1:2.15e19. This fails because it exceeds the maximum number of elements in a vector. However, 1:2.15^18 does not (though may crash your computer!). This indicates to me that MATLAB is approaching the limit quantity of elements in a vector when trying to run the 1:inf FOR loop. Thus MATLAB, truncates it prematurely (rather than run the expected &quot;infinite&quot; for loop).

Whereas, the while loop can continue on until it overflows the double.</description>
		<content:encoded><![CDATA[<p>I get the same limit for FOR loops. While I&#8217;m no expert, I hypothesize that the difference occurs between For and While because of variable declaration sizes. Consider these two loops:</p>
<p>for x=0:inf<br />
end</p>
<p>x = 0<br />
while 1<br />
   x = x +1<br />
end</p>
<p>The for loop terminates quite early with x equal to the 2.15e19 constant you speak of. However, the while loop can march on much past it.</p>
<p>My guesstimation is that MATLAB allocates all of the FOR loop values (the vector 1:inf) at once. The size of which reaches the MATLAB limit for number of elements in a vector. So I assume MATLAB, truncates the size of the vector to this limitation.</p>
<p>Meanwhile, the while loop only deals with a 1&#215;1 double and will only error out when it reaches the maximum value for a double.</p>
<p>Further proof of this is demonstrated when you try to create a vector 1:2.15e19. This fails because it exceeds the maximum number of elements in a vector. However, 1:2.15^18 does not (though may crash your computer!). This indicates to me that MATLAB is approaching the limit quantity of elements in a vector when trying to run the 1:inf FOR loop. Thus MATLAB, truncates it prematurely (rather than run the expected &#8220;infinite&#8221; for loop).</p>
<p>Whereas, the while loop can continue on until it overflows the double.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Neuberger</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-25370</link>
		<dc:creator>Daniel Neuberger</dc:creator>
		<pubDate>Sat, 26 Jan 2008 01:27:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-25370</guid>
		<description>Sorry, about the weird formatting of the script above.  It should be:

&lt;code&gt;
for x = 1:inf
end
a = x;
for x = 1:a*10
end
b = x;
x = 1;
while x&lt;a*10
    x = x + 1;
end
c = x;
x = 1;
while x&lt;inf
    x = x + 1;
end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Sorry, about the weird formatting of the script above.  It should be:</p>
<p><code><br />
for x = 1:inf<br />
end<br />
a = x;<br />
for x = 1:a*10<br />
end<br />
b = x;<br />
x = 1;<br />
while x&lt;a*10<br />
    x = x + 1;<br />
end<br />
c = x;<br />
x = 1;<br />
while x&lt;inf<br />
    x = x + 1;<br />
end<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Neuberger</title>
		<link>http://blogs.mathworks.com/loren/2006/07/19/how-for-works/#comment-25369</link>
		<dc:creator>Daniel Neuberger</dc:creator>
		<pubDate>Sat, 26 Jan 2008 01:25:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=46#comment-25369</guid>
		<description>A for loop won&#039;t actually go to infinity; on my computer it won&#039;t go past 2.1475e+009.  Take the following script:
&lt;code&gt;
for x = 1:inf
end
a = x;

for x = 1:a*10
end
b = x;

x = 1;
while x&lt;a*10
    x = x + 1;
end
c = x;

x = 1;
while x&lt;inf
    x = x + 1;
end
&lt;/code&gt;

I let it run for a long period of time, and then did a CTRL-C.  It was still running on the last while loop and the variable values where:

a = 2.1475e+009
b = 2.1475e+009
c = 2.1475e+010
x = 9.1499e+010

It seems as though there is some constraint on the number of iterations on a for loop, but not a while loop.  I can&#039;t explain exactly where the constraint comes from or why it&#039;s there, but I certainly can&#039;t find a way to beat it.

Interestingly, I discovered this because I asserted to someone that, in MATLAB, there are some things that a while loop could do, but not a for loop.  Of course, in most languages, both are equivalent though optimized for different uses.  Then someone reminded me of the ability to go to infinity.  When I tested it though, it didn&#039;t work.

Anyone know why?</description>
		<content:encoded><![CDATA[<p>A for loop won&#8217;t actually go to infinity; on my computer it won&#8217;t go past 2.1475e+009.  Take the following script:<br />
<code><br />
for x = 1:inf<br />
end<br />
a = x;</code></p>
<p>for x = 1:a*10<br />
end<br />
b = x;</p>
<p>x = 1;<br />
while x&lt;a*10<br />
    x = x + 1;<br />
end<br />
c = x;</p>
<p>x = 1;<br />
while x&lt;inf<br />
    x = x + 1;<br />
end<br />
</p>
<p>I let it run for a long period of time, and then did a CTRL-C.  It was still running on the last while loop and the variable values where:</p>
<p>a = 2.1475e+009<br />
b = 2.1475e+009<br />
c = 2.1475e+010<br />
x = 9.1499e+010</p>
<p>It seems as though there is some constraint on the number of iterations on a for loop, but not a while loop.  I can&#8217;t explain exactly where the constraint comes from or why it&#8217;s there, but I certainly can&#8217;t find a way to beat it.</p>
<p>Interestingly, I discovered this because I asserted to someone that, in MATLAB, there are some things that a while loop could do, but not a for loop.  Of course, in most languages, both are equivalent though optimized for different uses.  Then someone reminded me of the ability to go to infinity.  When I tested it though, it didn&#8217;t work.</p>
<p>Anyone know why?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

