help-octave
[Top][All Lists]
Advanced

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

Re: 'for' loop on elements of cell array (octave-3.4.2) - is it the way


From: Sergei Steshenko
Subject: Re: 'for' loop on elements of cell array (octave-3.4.2) - is it the way it is supposed to be ?
Date: Thu, 2 Feb 2012 06:56:41 -0800 (PST)




----- Original Message -----
> From: Ben Abbott <address@hidden>
> To: Sergei Steshenko <address@hidden>
> Cc: "address@hidden" <address@hidden>
> Sent: Thursday, February 2, 2012 3:47 PM
> Subject: Re: 'for' loop on elements of cell array (octave-3.4.2) - is it the 
> way it is supposed to be ?
> 
> On Feb 2, 2012, at 6:39 AM, Sergei Steshenko wrote:
> 
>>  Hello,
>> 
>>  I have tried the following piece of code:
>> 
>>  "
>>  octave:1> foolist = {1, 2, 5}
>>  foolist =
>>  {
>>    [1,1] =  1
>>    [1,2] =  2
>>    [1,3] =  5
>>  }
>>  octave:2> for foo = foolist foo endfor
>>  foo =
>>  {
>>    [1,1] =  1
>>  }
>>  foo =
>>  {
>>    [1,1] =  2
>>  }
>>  foo =
>>  {
>>    [1,1] =  5
>>  }
> 
> <snip>
> 
>>  In other words, the output from the 'for' loop doesn't look 
> like I expect, and if my expectations (I expected '1, 2, 5' sequence) 
> are wrong, which part of GNU Octave documentations explains how the output 
> should look ?
> 
> I'm confused. The output is 1, 2, 5.
> 
> The for loop below will run 3 times (once for each column)
> 
> for a = [1 2 3; 4 5 6; 7 8 9]
>   disp (a)
>   disp ('---')
> end
>    1
>    4
>    7
> ---
>    2
>    5
>    8
> ---
>    3
>    6
>    9
> ---
> 
> This one will only perform one loop
> 
> for a = [1 2 3; 4 5 6; 7 8 9](:)
>   disp (a)
>   disp ('---')
> end
>    1
>    4
>    7
>    2
>    5
>    8
>    3
>    6
>    9
> ---
> 
> And this one will perform 9 loops
> 
> for a = [1 2 3; 4 5 6; 7 8 9](:)'
>   disp (a)
>   disp ('---')
> end
> 1
> ---
> 4
> ---
> 7
> ---
> 2
> ---
> 5
> ---
> 8
> ---
> 3
> ---
> 6
> ---
> 9
> ---
> 
> Does that clarify the behavior ? 
> 
> Ben

Alas, it doesn't clarify the behavior. Please has a look at the following:

"
octave:1> foolist = {1, 2, 5}
foolist =
{
  [1,1] =  1
  [1,2] =  2
  [1,3] =  5
}
octave:2> for foo = foolist foo endfor
foo =
{
  [1,1] =  1
}
foo =
{
  [1,1] =  2
}
foo =
{
  [1,1] =  5
}
octave:3> for foo = foolist foo{:} endfor
ans =  1
ans =  2
ans =  5
octave:4>   
".

I am specifically confused regarding, say,

"
foo =
{
  [1,1] =  1
}
"

output - 'foo' is displayed like a cell array, though I expected it to be 
displayed as just a _number_.

In the second loop, where I have 'foo{:}', I do get plain _numbers_, but at the 
price of appending '{:}' to 'foo'.

In other words, I expected output I see in the second loop to be seen in the 
first loop.

Please note that I do _not_ use 'disp' statement and that my 'for' loops are 
one-liners.

Thanks,
  Sergei.




>


reply via email to

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