[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: numel(foo{:}) - feature or bug ?
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: numel(foo{:}) - feature or bug ? |
Date: |
Fri, 5 Aug 2011 10:32:17 -0500 |
On 5 August 2011 10:14, Sergei Steshenko <address@hidden> wrote:
>
>
> --- On Fri, 8/5/11, Ben Abbott <address@hidden> wrote:
>
>> From: Ben Abbott <address@hidden>
>> Subject: Re: numel(foo{:}) - feature or bug ?
>> To: "Sergei Steshenko" <address@hidden>
>> Cc: address@hidden
>> Date: Friday, August 5, 2011, 5:56 AM
>> On Aug 5, 2011, at 7:23 AM, Sergei
>> Steshenko wrote:
>>
>> > Hello,
>> >
>> > first here is screen output of 'help numel' in
>> octave-3.4.2:
>> >
>> > "
>> > octave:1> help numel
>> > `numel' is a built-in function
>> >
>> > -- Built-in Function: numel (A)
>> > -- Built-in Function: numel (A, IDX1, IDX2,
>> ...)
>> > Return the number of elements
>> in the object A. Optionally, if
>> > indices IDX1, IDX2, ... are
>> supplied, return the number of
>> > elements that would result
>> from the indexing
>> >
>> > A(IDX1,
>> IDX2, ...)
>> >
>> > This method is also called
>> when an object appears as lvalue with
>> > cs-list indexing, i.e.,
>> `object{...}' or `object(...).field'.
>> >
>> > See also: size
>>
>> In case there is some confusion about what fun(foo{:}) does
>> ...
>>
>> fun(foo{:})
>>
>> is the same as ...
>>
>> fun (foo{1}, fun{2}, ..., fun{end})
>>
>> Thus ...
>>
>> >> foo{1} = "a";
>> >> foo{2} = "ab";
>> >> foo{3} = "abc";
>> >> foo{4} = [1 2 3 4; 5 6 7 8; 9 10 11 12];
>> >>
>> >> numel (foo{:})
>> ans = 72
>> >> numel (foo{1})
>> ans = 1
>> >> numel (foo{1}, foo{2}, foo{3}, foo{4})
>> ans = 72
>>
>> foo{2:4} constitute 2, 3, 3*4 indices. Thus the total
>> number if implied indices is 2*3*3*4 = 72
>>
>> Ben
>>
>
> Somebody has already confirmed that Matlab gives the same result, so I'll
> have to agree that it's the "truth".
>
> But I consider it to be a silly truth.
>
> My simplistic understanding of cell arrays is that they are like matrices,
> just their elements can be of different types (and thus sizes).
>
> The following example is even more confusing:
> "
> octave:1> foo2d{1,1} = "a"
> foo2d =
> {
> [1,1] = a
> }
> octave:2> foo2d{1,2} = "aa"
> foo2d =
> {
> [1,1] = a
> [1,2] = aa
> }
> octave:3> foo2d{2,1} = "b"
> foo2d =
> {
> [1,1] = a
> [2,1] = b
> [1,2] = aa
> [2,2] = [](0x0)
> }
> octave:4> size(foo2d)
> ans =
>
> 2 2
>
> octave:5> numel(foo2d{:})
> ans = 0
> octave:6> foo2d
> foo2d =
> {
> [1,1] = a
> [2,1] = b
> [1,2] = aa
> [2,2] = [](0x0)
> }
> octave:7> foo2d{2,2} = [1 2 3 4 5; 6 7 8 9 10]
> foo2d =
> {
> [1,1] = a
> [2,1] = b
> [1,2] = aa
> [2,2] =
>
> 1 2 3 4 5
> 6 7 8 9 10
>
> }
> octave:8> size(foo2d)
> ans =
>
> 2 2
>
> octave:9> numel(foo2d{:})
> ans = 20
> octave:10>
> ".
>
> First, in
>
> "
> octave:5> numel(foo2d{:})
> ans = 0
> "
>
> zero number of elements which is simply false, i.e. _total_ number of
> elements, as 'help numel' says, is definitely _not_ zero.
It's not false. You seem to have misunderstood what foo2d{:} is, i.e.
its type is cs-list, not cell.
http://www.gnu.org/software/octave/doc/interpreter/Comma-Separated-Lists-Generated-from-Cell-Arrays.html#Comma-Separated-Lists-Generated-from-Cell-Arrays
What I agree could use an explanation is that the indices passed to
numel may not necessarily be numerical indices, but rather size() is
called on them. I'll update numel's docstring.
HTH,
- Jordi G. H.
In this regard
Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05
Re: numel(foo{:}) - feature or bug ?, Ben Abbott, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?,
Jordi Gutiérrez Hermoso <=
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?, Marco atzeri, 2011/08/05
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/06
- Re: numel(foo{:}) - feature or bug ?, Marco atzeri, 2011/08/06
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/06
- Re: numel(foo{:}) - feature or bug ?, Marco atzeri, 2011/08/06
- Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/06
Re: numel(foo{:}) - feature or bug ?, Jordi Gutiérrez Hermoso, 2011/08/05
Re: numel(foo{:}) - feature or bug ?, Sergei Steshenko, 2011/08/05