MathGroup Archive 2005

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

Search the Archive

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


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


  • Prev by Date: Converting result to ASCII-art
  • Next by Date: Re: NSum: badly missed Option
  • Previous by thread: Re: How to quickly find number of non-zero elements in sparse matrix rows?
  • Next by thread: meaning of a "*" in search string?