Iterating over non-zero entries of a sparse matrix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Iterating over non-zero entries of a sparse matrix

Christoph Ortner
Is there as iterator implemented that allows me to iterate over all non-zero entries of a sparse matrix or vector? E.g. 

for (i, j, z) in nonzeros(A) 


(I realise that nonzeros does something else!)

Reply | Threaded
Open this post in threaded view
|

Re: Iterating over non-zero entries of a sparse matrix

randmstring
Use findnz.

Am Mittwoch, 9. November 2016 14:37:11 UTC+1 schrieb Christoph Ortner:
Is there as iterator implemented that allows me to iterate over all non-zero entries of a sparse matrix or vector? E.g. 

for (i, j, z) in nonzeros(A) 


(I realise that nonzeros does something else!)

Reply | Threaded
Open this post in threaded view
|

Re: Iterating over non-zero entries of a sparse matrix

Milan Bouchet-Valat
In reply to this post by Christoph Ortner
Le mercredi 09 novembre 2016 à 05:37 -0800, Christoph Ortner a écrit :
> Is there as iterator implemented that allows me to iterate over all
> non-zero entries of a sparse matrix or vector? E.g. 
>
> for (i, j, z) in nonzeros(A) 
>
>
> (I realise that nonzeros does something else!)
As the docs for nonzeros() say, have a look at nzrange().


Regards
Reply | Threaded
Open this post in threaded view
|

Re: Iterating over non-zero entries of a sparse matrix

Christoph Ortner
missed that - thank you.

Reply | Threaded
Open this post in threaded view
|

Re: Iterating over non-zero entries of a sparse matrix

Christoph Ortner
So `findnz` converts the matrix to a tuple of arrays (triplet), and it creates a copy of all data.

So it is not quite what I was looking for. An iterator would be better.
Reply | Threaded
Open this post in threaded view
|

Re: Iterating over non-zero entries of a sparse matrix

Christoph Ortner
nzrange will work but is not as convenient.