help-octave
[Top][All Lists]

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

 From: James Sherman Jr. 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 10:09:52 -0500

On Thu, Feb 2, 2012 at 9:56 AM, Sergei Steshenko wrote:

----- Original Message -----
> 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.

>
_______________________________________________
Help-octave mailing list
https://mailman.cae.wisc.edu/listinfo/help-octave

The behavior is consistent, as far as I can tell.  Look at the columns of your foolist:

octave:9> foolist = {1,2,5}
foolist =
{
[1,1] =  1
[1,2] =  2
[1,3] =  5
}
octave:10> foolist(:,1)
ans =
{
[1,1] =  1
}
octave:11> foolist(:,2)
ans =
{
[1,1] =  2
}
octave:12> foolist(:,3)
ans =
{
[1,1] =  5
}

Each loop of your for statement output the corresponding column of foolist.

Hope this helps.