Re: Re: How to quickly find number of non-zero elements in sparse matrix rows?

*To*: mathgroup at smc.vnet.net*Subject*: [mg56920] Re: [mg56900] Re: How to quickly find number of non-zero elements in sparse matrix rows?*From*: Sseziwa Mukasa <mukasa at jeol.com>*Date*: Tue, 10 May 2005 03:42:24 -0400 (EDT)*References*: <d5hum0$k03$1@smc.vnet.net> <d5j5ec$qdr$1@smc.vnet.net> <200505090546.BAA13904@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On May 9, 2005, at 1:46 AM, Paul Abbott wrote: > In article <d5j5ec$qdr$1 at smc.vnet.net>, > Paul Abbott <paul at physics.uwa.edu.au> wrote: > >> For the full array, the following is quite reasonable: >> >> Most[Length /@ Split[ArrayRules[sa], #1[[1,1]]==#2[[1,1]]&]]; >> //Timing > > Chris Chiasson <chris.chiasson at gmail.com> pointed out that my code > does not work > if the array has all zero rows. This is easy to fix: > > {#[[1,1,1]],Length[#]}& /@ > Split[ArrayRules[sa], #1[[1,1]]==#2[[1,1]]& ] // Most > > returns the row number and number of non-zero entries in sa. > Just for completeness' sake I timed a similar expression: {#[[1, 1]], Length[#]} & /@ Split[Sort[#[[1]] & /@ Most[ ArrayRules[sa]]], #1[[1]] == #2[[1]] &] over a 200000 by 200000 array and it took about 5 seconds on a 1GHz G4 which seems reasonable. Regards, Ssezi

**References**:**Re: How to quickly find number of non-zero elements in sparse matrix rows?***From:*Paul Abbott <paul@physics.uwa.edu.au>