[SciPy-user] extracting elements of a matrix using arrays as indices

Robin robince@gmail....
Wed May 21 09:06:52 CDT 2008


On Tue, May 20, 2008 at 9:43 PM, Michael Hearne <mhearne@usgs.gov> wrote:
> Neilen - Thanks.  Unfortunately, I've either discovered a bug or have failed
> to understand how to use this function.  The below code snippet:
> from pylab import *
> import numpy
> print numpy.__version__, numpy.__file__
> data = rand(648,690)
> i,j = (data < 0.14).nonzero()
> data[ix_(i,j)] = data[ix_(i,j)]*0
> print 'No crash.'

If you're just trying to zero out values less than 0.14 (that's what
it looks like to me) you could try this:

data = rand(648,690)
data[data<0.14] = 0

which should be quicker and not crash!

I tried data[ix_(i,j)] = 0 (not sure why you need to multiply itself
by zero - you can just assign the value 0 directly) but it appears to
be very slow so if you can use the boolean indexing it will probably
be better.

Cheers

Robin


More information about the SciPy-user mailing list