Re: HELP: How to do count to the SUB-LIST
- To: mathgroup at smc.vnet.net
- Subject: [mg82193] Re: HELP: How to do count to the SUB-LIST
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 14 Oct 2007 06:15:32 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <fen5th$4ho$1@smc.vnet.net>
zhen wrote:
> Hallo, everyone
>
> I am new to Mathematica, and I get one problem now, hope to get some
> help here
>
> I got a list like this:
>
> {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 1, 0, 0, 0},
> {0, 0, 0, 0, 1, 1, 0, 0, 2, 0}, {1, 0, 1, 0, 0, 1, 0, 1, 0, 0},
> {1, 3, 3, 4, 2, 2, 5, 3, 2, 1}, {7, 2, 2, 3, 4, 4, 0, 3, 1, 3}}
>
> I need to get a list to show the number of non zero number in each sub
> list, as to say, I need to get a list like this:
>
> {0,3,3,4,10,9}
For non-binary data, you could use one of the following:
In[1]:= lst = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 0, 0, 1,
0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 2, 0}, {1, 0, 1, 0, 0, 1, 0, 1,
0, 0}, {1, 3, 3, 4, 2, 2, 5, 3, 2, 1}, {7, 2, 2, 3, 4, 4, 0, 3,
1, 3}};
Length /@ (lst /. 0 -> Sequence[])
Length /@ DeleteCases[lst, 0, {2}]
Plus @@@ Unitize /@ lst
Out[2]= {0, 3, 3, 4, 10, 9}
Out[3]= {0, 3, 3, 4, 10, 9}
Out[4]= {0, 3, 3, 4, 10, 9}
Regards,
--
Jean-Marc