Re: Re: How to do count for sub list?????
- To: mathgroup at smc.vnet.net
- Subject: [mg82203] Re: [mg82160] Re: [mg82095] How to do count for sub list?????
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Sun, 14 Oct 2007 06:20:40 -0400 (EDT)
- References: <200710120650.CAA04282@smc.vnet.net> <16695328.1192265948343.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
If all the numbers are 0 or 1, you can Map Total: list = {{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}}; Total /@ list {0, 0, 2, 1, 0, 0, 1, 1} The more general situation can be reduced to that using Unitize: list = RandomInteger[{-1, 1}, {10, 10}] {{-1, 0, 1, 1, 0, 0, 0, 0, -1, 1}, {0, 1, 0, 1, 1, -1, -1, 0, -1, -1}, {-1, 0, 1, 0, 1, -1, -1, -1, 0, 0}, {1, 0, 0, 1, 0, 0, -1, 1, 1, -1}, {0, 1, 0, 1, -1, -1, -1, 1, 0, 0}, {1, 1, 1, 0, 1, 1, -1, 0, -1, 0}, {0, -1, 1, 0, 1, 1, -1, -1, 0, -1}, {1, 0, 1, 0, 0, -1, 0, 1, -1, 1}, {-1, -1, 0, -1, -1, 0, 0, 1, 1, -1}, {0, -1, 1, 0, 0, 1, -1, -1, 1, -1}} Total /@ Unitize@list {5, 7, 6, 6, 6, 7, 7, 6, 7, 7} I suspect this is faster than using Count. This counts non-zeroes by columns: Total@Unitize@list {6, 6, 6, 5, 6, 7, 7, 7, 7, 7} Bobby On Sat, 13 Oct 2007 02:59:38 -0500, Ricardo Samad <resamad at gmail.com> wrote: > Hi, > > name your list: > > l = {{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}}; > > and use > > Table[Length[l[[n]]] - Length[Position[l[[n]], 0]], {n, 1, Length[l]}] > > or > > Length[#] - Length[Position[#, 0]] & /@ l > > > By the way, the result is {0, 0, 2, 1, 0, 0, 1, 1} and not > {0,0,1,1,0,0,1,1= > } > as written in your e-mail. > > Ricardo > > > > > On 10/12/07, wangzhen0829 at gmail.com <wangzhen0829 at gmail.com> wrote: >> >> Hallo, everyone >> >> 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} >> >> Thank you! >> >> >> > > > -- > ____________________________________ > Ricardo Elgul Samad > > tel: (+55 11) 3133-9372 > fax: (+55 11) 3133-9374 > > Centro de Lasers e Aplica=E7=F5es > IPEN/CNEN-SP > AV. Prof. Lineu Prestes 2242 > Cidade Universit=E1ria > 05508-000 > S=E3o Paulo - SP > Brazil > ____________________________________ > > > -- DrMajorBob at bigfoot.com
- References:
- How to do count for sub list?????
- From: wangzhen0829@gmail.com
- How to do count for sub list?????