[SciPy-user] extracting elements of a matrix using arrays as indices (SEGFAULT!)
Robin
robince@gmail....
Wed May 21 12:08:45 CDT 2008
On Wed, May 21, 2008 at 4:29 PM, Stéfan van der Walt <stefan@sun.ac.za> wrote:
> Hi Mike
>
> 2008/5/21 Michael Hearne <mhearne@usgs.gov>:
>> 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
>>
>> I get another segmentation fault.
>
> I can confirm that this bug exists in r5120.
Should this even work? While it shouldn't segfault - I don't think it
will do what is expected. I think it works for slices but when fancy
indexing is used there is some __setitem__ trick if I remember
correctly that doesn't work if you index twice.
Michael - is there a reason you really need to keep i and j indices
seperate or is it left over from Matlab? As Alan suggested you can use
z = (data < 0.14).nonzero() or just index directly with boolean
indexing. You can do operations involving the original array as well
with boolean indexing:
idx = data < 0.14
data[idx] *= 0 (in place multiplication)
or
data[idx] = 100*data[idx] + data[idx]**2
Even if the segfault you are seeing if fixed, I doubt if double
indexing like that is the best way to achieve what your trying to do.
Cheers
Robin
More information about the SciPy-user
mailing list