Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
For loop behavior

Subject: For loop behavior

From: Alex

Date: 24 Sep, 2012 04:28:08

Message: 1 of 3

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 non-relevant parts omitted):

    n = 3;
    for k = n:-1:1
        fprintf('k = %d\n', k)
        for j = k+1:n
            fprintf('\tj = %d\n', j)
        end
    end

The output for this is:
k = 3
k = 2
j = 3
k = 1
j = 2
j = 3

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!

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.

Thank you.

Subject: For loop behavior

From: Steven_Lord

Date: 24 Sep, 2012 13:32:28

Message: 2 of 3



"Alex " <john.public1@comcast.net> wrote in message
news:k3ongo$jl6$1@newscl01ah.mathworks.com...
> 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
> non-relevant parts omitted):
>
> n = 3;
> for k = n:-1:1
> fprintf('k = %d\n', k)
> for j = k+1:n
> fprintf('\tj = %d\n', j)
> end
> end
>
> The output for this is:
> k = 3
> k = 2
> j = 3
> k = 1
> j = 2
> j = 3
>
> What I don't understand is why the outer loop value (k) gets decremented
> before the inner loop runs even once?

Essentially, a FOR loop that looks like:

for m = a:d:b
    % do stuff
end

executes once for each element of the vector a:d:b (and if d is omitted, it
defaults to 1.) In your case, in your first iteration through the outer
loop, n = 3 and k = 3. Therefore the inner loop will execute once per
element of 4:3 (since 4 == k+1.) What is the vector 4:3?

>> x = 4:3
x =
   Empty matrix: 1-by-0

This vector has 0 columns because you can't get to 3 by starting at 4 and
incrementing by the default d value of 1.

Therefore, at the first outer loop iteration, the inner loop is executed 0
times.

At the next outer loop iteration, the inner loop executes once per element
of 3:3.

>> x = 3:3
x =
     3

At the third (and in this situation final) outer loop iteration, the inner
loop executes once per element of 2:3.

>> x = 2:3
x =
     2 3

> 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!
>
> 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.

http://www.mathworks.com/help/matlab/ref/for.html

See the first and second rows in the table in the Description section.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: For loop behavior

From: Ryan

Date: 21 Oct, 2012 18:45:08

Message: 3 of 3

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.



"Alex" wrote in message <k3ongo$jl6$1@newscl01ah.mathworks.com>...
> 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 non-relevant parts omitted):
>
> n = 3;
> for k = n:-1:1
> fprintf('k = %d\n', k)
> for j = k+1:n
> fprintf('\tj = %d\n', j)
> end
> end
>
> The output for this is:
> k = 3
> k = 2
> j = 3
> k = 1
> j = 2
> j = 3
>
> 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!
>
> 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.
>
> Thank you.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us