473,739 Members | 4,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7644
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_matr ix((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_matr ix, 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_matr ix((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
14246
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 help me? Thanks in advance. I write this, but it's not so good :(
7
2663
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
3882
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 use and add this function in my program. Could you help me please? Thank you for your help... #include <stdio.h> #include <math.h>
5
9715
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 implement the sparse matrices as a doubly-linked list, in which each non-zero cell is stored roughly as follows: int rownum int colnum
0
2725
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 threads on this board, but the people usually searching for such a lib don't seem to know what they exactly want and don't really seem to have specific. On the other hand, I know perfectly well what I am looking for: 1) I need a sparse CRS matrix...
4
3110
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 a sparse matrix and i don't need to parallelize it. i have explained how it works in case u need to use it or something... the second code is the one that needs to be parallelized. here's what the each of the 2 programs does: 1-the first program...
0
1281
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 (s.row > s.col) p = s.row; else p = s.col; matrix.headnode = new MatrixNode(FALSE, &s); if (p==0) {matrix.headnode->right = matrix.headnode; return is;} MatrixNodePtr *head = new MatrixNodePtr;
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.