MathGroup Archive 2005

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56881] Re: [mg56841] How to quickly find number of non-zero elements in sparse matrix rows?
  • From: Pratik Desai <pdesai1 at umbc.edu>
  • Date: Mon, 9 May 2005 01:46:02 -0400 (EDT)
  • References: <200505070817.EAA20272@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Matt Pharr wrote:

>I have a sparse matrix, roughly 200k by 200k, with about .01% of the
>entries non zero, represented with SparseArray.  I'd like to reasonably
>efficiently generate a 200k-long list where each element is the number of
>non-zero entries in the corresponding row of the matrix.  I haven't been
>able to figure out a quick way to do this.
>
>One approach I've tried is the following (using a made up SparseArray of
>an identity matrix to illustrate the point):
>
>In[76]:= sa = SparseArray[Table[{i,i}\[Rule]1, {i, 200000}]];
>In[77]:= rowLen[sa_, r_] :=  Length[ArrayRules[Take[sa, {r}]]]-1
>
>However, it's quite slow--about 1/10 of a second for each value computed
>(on a 1GHz Mac G4)
>
>In[80]:= Table[rowLen[sa,i], {i,100}] // Timing
>Out[80]=
>{12.4165 Second,{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
>    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
>    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
>
>I've got to assume that there's an efficient way to do this--does anyone
>have any suggestions?
>
>thanks,
>-matt
>  
>
Hi
You may have already try this approach, although quite naive, but seems 
to work for 2000X 2000 matrix on my
Mathematica 5.1.1, Celeron, XP
Of course it gives up  for 20000

Clear[sa, sas, sass]
Off[General::spell1]
sa = SparseArray[Table[{i, i} -> 1, {i, 2000}]] // Normal // Flatten;
sas = DeleteCases[sa, 0];
l = Length[sas]

-- 
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134



  • Prev by Date: Re: Calling a MS-DOS command
  • Next by Date: Re: Calling a MS-DOS command
  • Previous by thread: Re: How to quickly find number of non-zero elements in sparse matrix rows?
  • Next by thread: Re: How to quickly find number of non-zero elements in sparse matrix rows?