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

Michael Hearne mhearne at usgs.gov
Wed May 21 09:51:25 EDT 2008


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 realize that the sense of the arrays is backwards from Fabrice's  
example, but doing it the other way:
data[:,i][j,:]

gives me an index out of range error.

--Mike

On May 20, 2008, at 3:51 PM, Fabrice Silva wrote:

> Le mardi 20 mai 2008 à 08:03 -0600, Michael Hearne a écrit :
>> In Python:
>> a = array([[ 11, 12, 13, 14 ],
>> [ 21, 22, 23, 24 ],
>> [ 31, 32, 33, 34 ]])
>> a[[0,2],[0,3]]
>> gives you:
>> array([11, 34])
>
>> So, I have two questions:
>> 1) How do I actually get the equivalent behavior in Python?
>
>>>> from numpy import array
>>>> a = array([[ 11, 12, 13, 14 ],
> ... [ 21, 22, 23, 24 ],
> ... [ 31, 32, 33, 34 ]])
>
>>>> a[:,[0,3]][[0,2],:]
> array([[11, 14],
>       [31, 34]])
>
>>>> a[[0,2],[0,3]]
> array([11, 34])
>
> -- 
> Fabrice Silva <silva at lma.cnrs-mrs.fr>
> LMA UPR CNRS 7051 - équipe S2M
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user




------------------------------------------------------
Michael Hearne
mhearne at usgs.gov
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20080521/55369e14/attachment.html>


More information about the SciPy-User mailing list