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

Michael Hearne mhearne@usgs....
Tue May 20 09:03:36 CDT 2008

```All:  I'm porting some code from Matlab and I ran across a subtle
difference between it and Numpy.

In Matlab:

a = [ 11 12 13 14 ;
21 22 23 24 ;
31 32 33 34 ];

a([1 3],[1 4])

gives you:

11    14
31    34

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])

I tried to find an example of how to do this, and the normally very
has this:

a.take([0,2]).take([0,3], axis=1)

which gives me:

ValueError: axis(=1) out of bounds

So, I have two questions:

1) How do I actually get the equivalent behavior in Python?
2) Does anyone know the best way to contact Vidar Gundersen, the
author of the above page to let him know about these errors?  It
really is a useful resource, and I'd hate to have it continue to live
with errors in it...

--Mike

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

```