Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to do count for sub list?????

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82192] Re: How to do count for sub list?????
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 14 Oct 2007 06:15:01 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fen5rk$4gi$1@smc.vnet.net>

wangzhen0829 at gmail.com wrote:

> I am new to Mathematica, and now I have a problem with it, hope to get
> some help from here.
> 
> I have a
> 
> {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {
>   0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {
>   0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {
>   0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}}
> 
> I need to get a list which represent the number of NON-zero number in
> each sublist.
> 
> I should get a list like:
> 
> {0,0,1,1,0,0,1,1}
-------^
I believe that the third entry should be a two rather than a one.

Assuming that you want to count the numbers of ones within each 
sublists, you could use, among many other way, one of the following:

In[1]:= lst = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0,
     0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0,
      0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0,
     0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0,
      0}};

Count[#, 1] & /@ lst

Total[Transpose@lst]

Plus @@@ lst

Out[2]= {0, 0, 2, 1, 0, 0, 1, 1}

Out[3]= {0, 0, 2, 1, 0, 0, 1, 1}

Out[4]= {0, 0, 2, 1, 0, 0, 1, 1}

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: Options in ListContourPlot
  • Next by Date: Re: Programming Euler's Method into Mathematica
  • Previous by thread: Re: How to do count for sub list?????
  • Next by thread: Unwanted resize of frontend window