[SciPy-user] extracting elements of a matrix using arrays as indices (SEGFAULT!)

Travis E. Oliphant oliphant@enthought....
Wed May 21 23:00:45 CDT 2008


Michael Hearne wrote:
> Thanks for the help - the method below works just fine in my real 
> application (which has nothing to do with setting elements of an array 
> to zero - that was just a simple example of something to _do_ with the 
> data being indexed).
>
> However, I am still concerned about the larger problem of getting a 
> segfault using ANY method of indexing an array.  If it is user error 
> that is causing the problem, then shouldn't I get an exception that 
> tells me my syntax is somehow incorrect?
>
> I've added SEGFAULT to the subject line in hopes that someone 
> responsible for the core NumPy code (Travis O., perhaps?) will take 
> notice and address the issue.  If that happens, once again, I'm happy 
> to help test on my version of NumPy wherever needed.
>
> Thanks for all the suggestions,
>
> Mike
> On May 21, 2008, at 8:10 AM, Alan McIntyre wrote:
>
>> On Wed, May 21, 2008 at 9:51 AM, Michael Hearne <mhearne@usgs.gov 
>> <mailto:mhearne@usgs.gov>> wrote:
>>> If I try that on a larger example, similar to the second one I made
>>> yesterday:
>>> nrows = 648
>>> ncols = 690
>>> data = rand(nrows,ncols)
>>> i,j = (data < 0.14).nonzero()
>>> data[i,:][:,j] = data[i,:][:,j]*0
Just a suggestion, but you will get the attention of the NumPy 
developers more quickly on the numpy-discussion@scipy.org mailing list

This kind of indexing is going to be very slow and it won't do what you 
want.

A couple of points:

data[i,:] will return a copy and then [:,j] will set values into that 
copy of the data (which is then not bound to anything so you lose it).   
I can't think of any example that I would use

data[<something>][<something>] = <something>

You are right that it should not segfault.   How much memory do you 
have?     My first guess is that you are running out of memory and there 
is some malloc that is not being checked correctly,   If you can run the 
code under gdb and give a backtrace when the error occurs it would be 
very helpful to trac it down.

Thanks,

-Travis



More information about the SciPy-user mailing list