octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

For loops and column vectors


From: John W. Eaton
Subject: For loops and column vectors
Date: Sun, 17 Jul 2011 14:09:14 -0400

On 17-Jul-2011, Rik wrote:

| 6/17/11
| 
| All,
| 
| I came across what I consider to be odd behavior, but perhaps we are trying
| to emulate Matlab or perhaps someone knows a better reason for this.  In
| short, the for loop construct works correctly when the range expression is
| a row vector but not when it is a column vector.
| 
| Sample Code:
| for i = [1:3]
|   disp (i);
|   disp ("--");
| endfor
| 
| Output:
|  1
| --
|  2
| --
|  3
| --
| 
| for i = [1:3]'
|   disp (i);
|   disp ("--");
| endfor
| 
| Output:
|    1
|    2
|    3
| --
| 
| In the second case the loop variable gets assigned the entire value of the
| column vector and the loop iterates just once.
| 
| Any ideas?

Like Ben said, this is the way for loops work in Matlab.  It might
make more sense if you know that in "for I = M ... end", the columns
of M are assigned one at a time to the iteration variable I.  So then
the behavior is consistent for row and column vectors, matrices, and
N-d arrays.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]