473,379 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Finding Nonzero Elements in a Sparse Matrix

Hi,

Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?

Cheers.

Nov 7 '06 #1
4 7613
The function you might want is nonzero() or flatnonzero()
>>from numpy import *
>>a=array([ [1,2],[0,4] ])
>>a
array([[1, 2],
[0, 4]])
>>flatnonzero(a)
array([0, 1, 3])

nonzero() will return the a sequence of index arrays of non zero
elements
flatnonzero() returns the non-zero elements of the flattened version
of the array.

Cheers,
Nick Vatamaniuc

deLenn wrote:
Hi,

Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?

Cheers.
Nov 7 '06 #2
Thanks for the reply.

'nonzero' deos not seem to work with sparse matrices. here is an
example:
from scipy import *
A = sparse.lil_matrix((3,3))
A[1,2] = 10
A[2,0] = -10

nonzero(A)
>>()

(I tried it with an ordinary matrix, and it works fine)

Cheers.





Nick Vatamaniuc wrote:
The function you might want is nonzero() or flatnonzero()
>from numpy import *
>a=array([ [1,2],[0,4] ])
>a
array([[1, 2],
[0, 4]])
>flatnonzero(a)
array([0, 1, 3])

nonzero() will return the a sequence of index arrays of non zero
elements
flatnonzero() returns the non-zero elements of the flattened version
of the array.

Cheers,
Nick Vatamaniuc

deLenn wrote:
Hi,

Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?

Cheers.
Nov 7 '06 #3
deLenn wrote:
Hi,

Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?
You will want to ask scipy questions on the scipy list.

http://www.scipy.org/Mailing_Lists

There is no explicit interface on sparse matrix objects to expose the indices of
the nonzero elements. A different implementation would have to be written for
each type of sparse matrix format. However, if one can spare the memory, one can
convert to the coordinate list format and read the row and column indices from
that object.
In [1]: from scipy.sparse import lil_matrix

In [2]: A = lil_matrix((3,3))

In [3]: A[1,2] = 10

In [4]: A[2,0] = -10

In [5]: Acoo = A.tocoo()

In [6]: Acoo.row
Out[6]: array([2, 1])

In [7]: Acoo.col
Out[7]: array([0, 2])
--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 7 '06 #4
de Lenn,

Sorry I assumed the nonzero would work for sparse matrices as well.

BUT! -- If the sparse matrix used is the default scipy's
sparse.lil_matrix, you just need to print out the representation
because the lil_matrix is implemented as a _sequence of non-zero
elements_ i.e. just what you need.

In other words it is kind of silly to provide a nonzero for lil_matrix
because it has _only_ non-zero elements.

Well, here is the example:
------------------------------------
>>from scipy import *
A=sparse.lil_matrix((3,3))
A[1,2]=10
A[2,0]=-10
print A
(1, 2) 10
(2, 0) -10
>>>
------------------------------------

The only way it could be helpful is if you get a lil_matrix returned as
an object from some code and you need to list all the elements...

Hope this helps,
Nick Vatamaniuc
deLenn wrote:
Thanks for the reply.

'nonzero' deos not seem to work with sparse matrices. here is an
example:
from scipy import *
A = sparse.lil_matrix((3,3))
A[1,2] = 10
A[2,0] = -10

nonzero(A)
>()


(I tried it with an ordinary matrix, and it works fine)

Cheers.





Nick Vatamaniuc wrote:
The function you might want is nonzero() or flatnonzero()
>>from numpy import *
>>a=array([ [1,2],[0,4] ])
>>a
array([[1, 2],
[0, 4]])
>>flatnonzero(a)
array([0, 1, 3])

nonzero() will return the a sequence of index arrays of non zero
elements
flatnonzero() returns the non-zero elements of the flattened version
of the array.

Cheers,
Nick Vatamaniuc

deLenn wrote:
Hi,
>
Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?
>
Cheers.
Nov 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mariaczi | last post by:
Hi, I code class to storage sparse matrix row compressed and i have a problem with implements method to setVal and addVal. I will replace later this methods overloaded operator(). Please, can You...
7
by: dolcetheking | last post by:
I have this sparse matrix |5| | | | | | |4| |7| | | | | | | | |1| |3| | | | | | | |2| and I need to make a vector V(6)={5,4,7,1,3,2} I DONT KNOW HOW TO DO THIS IN C++
6
by: hvmclrhu | last post by:
Hi I have a big problem. When we compile serial.c with gcc, I get this error program is generating the sparse matrix Segmentation fault I think ı have to use malloc() but I don't know how to...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
0
by: saschaM | last post by:
Hello folks :) I am working on a certain piece of code and i've been searching for a sparse matrix library for c/c++ for quite a while. I have been freelance searching in the web and for older...
4
by: kayjay66 | last post by:
hello! MY CODE IS RUNNING AND IT'S ALL FINE, I ONLY NEED TO KNOW HOW TO MODIFY IT AND MAKE IT RUN ON A CORE 2 DUO INTEL CENTRINO. i have written 2 programs, the first one is only for creating...
0
by: zahraZ | last post by:
hello every body code below is about reading sparse matrix. Anyone understand this code? istream& operator>>(istream& is, Matrix& matrix) { Triple s; int p; is >> s.row >> s.col >> s.value; if...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.