help-octave
[Top][All Lists]
Advanced

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

Re: numel(foo{:}) - feature or bug ?


From: Sergei Steshenko
Subject: Re: numel(foo{:}) - feature or bug ?
Date: Fri, 5 Aug 2011 08:23:34 -0700 (PDT)


--- On Fri, 8/5/11, Sergei Steshenko <address@hidden> wrote:

> From: Sergei Steshenko <address@hidden>
> Subject: Re: numel(foo{:}) - feature or bug ?
> To: "Ben Abbott" <address@hidden>
> Cc: address@hidden
> Date: Friday, August 5, 2011, 8:14 AM
> 
> 
> --- 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.
> 
> Secondly we have
> 
> "
> octave:9> numel(foo2d{:})
> ans =  20
> "
> 
> which, of course, is _not_ 2 * 2 = 4 which I would expect
> from
> 
> "
> octave:8> size(foo2d)
> ans =
> 
>    2   2
> ".
> 
> I don't know how Matlab behaves in this case.
> 
> And, most importantly, even if all this is the "truth", it
> should be
> documented.
> 
> Or blame me for not being a native English speaker and thus
> being
> unable to extract all this "truth" from the existing
> documentation.
> 
> ...
> 
> Had I been implementing cell arrays, I would have
> implemented them as
> N-dimensional arrays of pointers - each pointer pointing to
> the actual
> element, and thus for the above 2-d example I would have
> had, as for
> simple matrices, numel((foo2d{:}) == 4 == 2 * 2.
> 
> And for my original example I would have had 4 too.
> 
> Thanks,
>   Sergei.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
> 

You know what, even if to forget about my pointer thinking and stick with
human-visible objects, for my "bar" case:

"
octave:3> bar{3} = "def"
bar =
{
  [1,1] = d
  [1,2] = de
  [1,3] = def
}
octave:4> numel(bar{:})
ans =  6
octave:5> 
"

I would say numel(bar{:}) should be 3 - because my eyes see 3 strings, and
for my "foo" case:

"
octave:5> foo{4} = [1 2 3 4; 5 6 7 8; 9 10 11 12]
foo =
{
  [1,1] = a
  [1,2] = ab
  [1,3] = abc
  [1,4] =

      1    2    3    4
      5    6    7    8
      9   10   11   12

}
octave:6> numel(foo{:})
ans =  72
"

I would say number of elements is 15 - I see 3 strings and 3 x 4 matrix
having obviously 12 elements, so total is 15.

But, of course, Matlab compatibility ...

Regards,
  Sergei.





reply via email to

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