http://www.mathworks.com/matlabcentral/newsreader/view_thread/323305
MATLAB Central Newsreader  For loop behavior
Feed for thread: For loop behavior
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 24 Sep 2012 04:28:08 +0000
For loop behavior
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323305#887983
Alex
I'm new to matlab, and am trying to step through a program that was given to me. I have inserted a bunch of fprintf statements to trace variables, and am seeing behavior that I cannot understand. Here is the code (all nonrelevant parts omitted):<br>
<br>
n = 3;<br>
for k = n:1:1<br>
fprintf('k = %d\n', k)<br>
for j = k+1:n<br>
fprintf('\tj = %d\n', j)<br>
end<br>
end<br>
<br>
The output for this is:<br>
k = 3<br>
k = 2<br>
j = 3<br>
k = 1<br>
j = 2<br>
j = 3<br>
<br>
What I don't understand is why the outer loop value (k) gets decremented before the inner loop runs even once? And why is the inner loop only running once (or less) during each iteration of the outer loop? I thought that the line "for k = n:1:1" was the equivalent of "for (int i = n; n>0; n)" in C++. Apparently not!<br>
<br>
Can anybody explain this to me  or better yet, just point me to some documentation that explains it. I have searched around on for loops in matlab, but can't find this one.<br>
<br>
Thank you.

Mon, 24 Sep 2012 13:32:28 +0000
Re: For loop behavior
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323305#888004
Steven_Lord
<br>
<br>
"Alex " <john.public1@comcast.net> wrote in message <br>
news:k3ongo$jl6$1@newscl01ah.mathworks.com...<br>
> I'm new to matlab, and am trying to step through a program that was given <br>
> to me. I have inserted a bunch of fprintf statements to trace variables, <br>
> and am seeing behavior that I cannot understand. Here is the code (all <br>
> nonrelevant parts omitted):<br>
><br>
> n = 3;<br>
> for k = n:1:1<br>
> fprintf('k = %d\n', k)<br>
> for j = k+1:n<br>
> fprintf('\tj = %d\n', j)<br>
> end<br>
> end<br>
><br>
> The output for this is:<br>
> k = 3<br>
> k = 2<br>
> j = 3<br>
> k = 1<br>
> j = 2<br>
> j = 3<br>
><br>
> What I don't understand is why the outer loop value (k) gets decremented <br>
> before the inner loop runs even once?<br>
<br>
Essentially, a FOR loop that looks like:<br>
<br>
for m = a:d:b<br>
% do stuff<br>
end<br>
<br>
executes once for each element of the vector a:d:b (and if d is omitted, it <br>
defaults to 1.) In your case, in your first iteration through the outer <br>
loop, n = 3 and k = 3. Therefore the inner loop will execute once per <br>
element of 4:3 (since 4 == k+1.) What is the vector 4:3?<br>
<br>
>> x = 4:3<br>
x =<br>
Empty matrix: 1by0<br>
<br>
This vector has 0 columns because you can't get to 3 by starting at 4 and <br>
incrementing by the default d value of 1.<br>
<br>
Therefore, at the first outer loop iteration, the inner loop is executed 0 <br>
times.<br>
<br>
At the next outer loop iteration, the inner loop executes once per element <br>
of 3:3.<br>
<br>
>> x = 3:3<br>
x =<br>
3<br>
<br>
At the third (and in this situation final) outer loop iteration, the inner <br>
loop executes once per element of 2:3.<br>
<br>
>> x = 2:3<br>
x =<br>
2 3<br>
<br>
> And why is the inner loop only running once (or less) during each <br>
> iteration of the outer loop? I thought that the line "for k = n:1:1" was <br>
> the equivalent of "for (int i = n; n>0; n)" in C++. Apparently not!<br>
><br>
> Can anybody explain this to me  or better yet, just point me to some <br>
> documentation that explains it. I have searched around on for loops in <br>
> matlab, but can't find this one.<br>
<br>
<a href="http://www.mathworks.com/help/matlab/ref/for.html">http://www.mathworks.com/help/matlab/ref/for.html</a><br>
<br>
See the first and second rows in the table in the Description section.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Sun, 21 Oct 2012 18:45:08 +0000
Re: For loop behavior
http://www.mathworks.com/matlabcentral/newsreader/view_thread/323305#890016
Ryan
I didn't test your exact code, but I'll tell you, from experience, whenever I have to figure out things like this, I go into the Editor, set a break point, hit the Start/Play button, and then hit F11 over and over, stepping through every line of code, until I get to the point where the thing is breaking. Then, pay attention; focus like a laser beam. See what's REALLY going on, and you should easily deduce a solution to the issue at hand.<br>
<br>
<br>
<br>
"Alex" wrote in message <k3ongo$jl6$1@newscl01ah.mathworks.com>...<br>
> I'm new to matlab, and am trying to step through a program that was given to me. I have inserted a bunch of fprintf statements to trace variables, and am seeing behavior that I cannot understand. Here is the code (all nonrelevant parts omitted):<br>
> <br>
> n = 3;<br>
> for k = n:1:1<br>
> fprintf('k = %d\n', k)<br>
> for j = k+1:n<br>
> fprintf('\tj = %d\n', j)<br>
> end<br>
> end<br>
> <br>
> The output for this is:<br>
> k = 3<br>
> k = 2<br>
> j = 3<br>
> k = 1<br>
> j = 2<br>
> j = 3<br>
> <br>
> What I don't understand is why the outer loop value (k) gets decremented before the inner loop runs even once? And why is the inner loop only running once (or less) during each iteration of the outer loop? I thought that the line "for k = n:1:1" was the equivalent of "for (int i = n; n>0; n)" in C++. Apparently not!<br>
> <br>
> Can anybody explain this to me  or better yet, just point me to some documentation that explains it. I have searched around on for loops in matlab, but can't find this one.<br>
> <br>
> Thank you.