[Numpy-discussion] Matlab -> NumPy translation and indexing

Stefan van der Walt stefan@sun.ac...
Sat Mar 17 10:12:25 CDT 2007


On Thu, Mar 15, 2007 at 12:26:10PM +0100, David Koch wrote:
> Next thing, I have
> 
> A_Idx = array([1, 0])
> 
> and B is a matrix. How would I select the four elements in B indicated by
> "meshgrid(A_Idx)", that ist: B[1,1], B[1,0], B[0,1], B[0,0]
> In Matlab you would simply use B(A_idx, A_idx) whereas in NumPy B[A_Idx, A_idx]
> returns B[1,1] and B[0,0] only.
> 
> I read that A.take(A_idx).take(A_idx, axis = 1) should work but it
> doesn't.

In [12]: B[N.ix_(A_idx,A_idx)]
Out[12]: 
array([[4, 3],
       [2, 1]])

Cheers
Stéfan


More information about the Numpy-discussion mailing list