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:29:09

Message: 1 of 4

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: Bruno Luong

Date: 24 Sep, 2012 05:53:08

Message: 2 of 4

"Alex" wrote in message <k3onil$jo7$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!

k = 3, inner loop runs 0 time (i.e., not at all)
k = 2, inner loop runs 1 time
k = 1, inner loop runs 2 time

That's exactly what you get, logical, and conform. There is no mysterious behavior of for-loop as you believe.

Bruno

Subject: For loop behavior

From: David Epstein

Date: 25 Sep, 2012 02:28:08

Message: 3 of 4

"Alex" wrote in message <k3onil$jo7$1@newscl01ah.mathworks.com>...
> n = 3;
> for k = n:-1:1
> 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!

n:-1:1 means the vector [3,2,1]. k is set to each of these values, one at a time.
Type
doc colon
or
help colon
in the Command Window for a full explanation.

Subject: For loop behavior

From: David Epstein

Date: 25 Sep, 2012 11:38:08

Message: 4 of 4

"David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <k3r4rn$163$1@newscl01ah.mathworks.com>...
> "Alex" wrote in message <k3onil$jo7$1@newscl01ah.mathworks.com>...
> > n = 3;
> > for k = n:-1:1
> > 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!
>
> n:-1:1 means the vector [3,2,1]. k is set to each of these values, one at a time.
> Type
> doc colon
> or
> help colon
> in the Command Window for a full explanation.

The C++ equivalent is "for(int k=n;k>0;k--)", which is not what you wrote. If you tried to rewrite the matlab code in C++ using your "equivalent", the answers would be different.

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