Counting entries in cs-lists

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Counting entries in cs-lists

JokerOne
Hi community,


again, I am asking for your help regarding a cs-list issue:

Is there a one-line solution to count the entries of a cs-list, e.g.

>>> a(1).b.c = [1;2];
>>> a(2).b.c = [3;4;5];

Now:
desired_function_or_line(a.b.c)
is suppose to yield :
5

?

A loop will do of course. But I would like to check first, if there is anything already provided by Octave.

Thanks for your help.

Reply | Threaded
Open this post in threaded view
|

Re: Counting entries in cs-lists

Bård Skaflestad
On 22/04/15 12:09, JokerOne wrote:

> Is there a one-line solution to count the entries of a cs-list, e.g.
>
>>>> a(1).b.c = [1;2];
>>>> a(2).b.c = [3;4;5];
>
> Now:
> desired_function_or_line(a.b.c)
> is suppose to yield :
> 5

I don't have a one-liner, but the following produces the result that I
think you want.  Do note, however, that this sequence is not general as
it is *very* much dependent on knowing the exact shapes of your objects.
  The underlying technique is nevertheless applicable in other, similar
situations.

    ab  = [ a.b ];
    cnt = numel(vertcat(ab.c))


Sincerely,
--
Bård Skaflestad <[hidden email]>
SINTEF ICT, Applied Mathematics

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Counting entries in cs-lists

Carlo de Falco-2
In reply to this post by JokerOne

On 22 Apr 2015, at 12:09, JokerOne <[hidden email]> wrote:

> Hi community,
>
>
> again, I am asking for your help regarding a cs-list issue:
>
> Is there a one-line solution to count the entries of a cs-list, e.g.
>
>>>> a(1).b.c = [1;2];
>>>> a(2).b.c = [3;4;5];
>
> Now:
> desired_function_or_line(a.b.c)
> is suppose to yield :
> 5
>
> ?
>
> A loop will do of course. But I would like to check first, if there is
> anything already provided by Octave.
>
> Thanks for your help.

I'm not really sure what you are trying to do, but a.b.c is not a cs-list.
actually a.b is already a cs-list which you cannot index any further, so a.b.c is a syntax error.

c.





_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Counting entries in cs-lists

JokerOne
Thanks all for your help.

>>> numel(vertcat([a.b].c))
ans =  5

will even work as a one-liner then.

I will have a closer look to vertcat and hortcat, might be useful for some tasks.


Also, yes, I guess, I used the wrong term, sorry. What is a(1).b.c called ? An indexed cs-list? Whatever...
Generally, say f.g.h = 1 is possible, and not a syntax error
>>> f.g.h=1
f =

  scalar structure containing the fields:

    g =

      scalar structure containing the fields:

        h =  1

but of course after setting
>>>> a(1).b.c = [1;2];
>>>> a(2).b.c = [3;4;5];
,
a.b.c
IS a syntay error.

Thanks again for you very fast help.