[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: For loops and column vectors
From: |
Rik |
Subject: |
Re: For loops and column vectors |
Date: |
Sun, 17 Jul 2011 17:17:47 -0700 |
On 07/17/2011 11:09 AM, John W. Eaton wrote:
> 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.
Interesting. In my case the actual expression was
for i = find (vector)
...
endfor
and I was getting different behavior based on the orientation of vector. I
guess I'll just always reshape the vector before the call to find.
--Rik