[Numpy-discussion] On responding to dubious ideas (was: Re: Advanced indexing: "fancy" vs. orthogonal)

Pauli Virtanen pav at iki.fi
Fri Apr 10 06:50:50 EDT 2015


10.04.2015, 03:23, Alexander Belopolsky kirjoitti:
[clip]
> Suppose I have
> 
>>>> a
> array([[11, 12, 13, 14],
>        [21, 22, 23, 24],
>        [31, 32, 33, 34],
>        [41, 42, 43, 44]])
[clip]
> but if I try to do both, I get the diagonal instead
> 
>>>> a[[1,2],[1,2]]
> array([22, 33])

You want

from numpy import ix_, array
a = array([[11, 12, 13, 14],
          [21, 22, 23, 24],
          [31, 32, 33, 34],
          [41, 42, 43, 44]])
print(a[ix_([1,2], [1,2])])

What it ix_ actually does can be understood looking at

print(ix_([1,2],[1,2]))





More information about the NumPy-Discussion mailing list