Re: How to quickly find number of non-zero elements in sparse matrix rows?
- To: mathgroup at smc.vnet.net
- Subject: [mg56847] Re: How to quickly find number of non-zero elements in sparse matrix rows?
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Sat, 7 May 2005 15:35:08 -0400 (EDT)
- Organization: The University of Western Australia
- Sender: owner-wri-mathgroup at wolfram.com
Paul Abbott wrote:
> The 4-th part of the data structure records the cumulative sum of
> non-zero entries. However, there seems to be no simple way to access
> this information. Usually, for special formats you can use Part to
> extract such information but Part is interpreted by SparseArray,
> circumventing this.
Of course one can use pattern-matching! For a sparse array,
sa=SparseArray[{i_,i_} -> 1,{10,10}]
extract the cumulative sum of non-zero entries,
sa /. SparseArray[_, _, _, x_] :> x[[2,1]]
and use ListConvolve to give the number of non-zero entries in each row.
ListConvolve[{1, -1}, %]
I think that this is about as fast as you can hope for.
Cheers,
Paul
--
Paul Abbott Phone: +61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul
http://InternationalMathematicaSymposium.org/IMS2005/