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

Derek Homeier derek at astro.physik.uni-goettingen.de
Thu Apr 9 20:41:40 EDT 2015


On 10 Apr 2015, at 2:23 am, Alexander Belopolsky <ndarray at mac.com> wrote:
> 
> What I find somewhat annoying is the difficulty of simultaneously selecting  a subset of rows and columns from a given matrix.
> 
> Suppose I have
> 
> >>> a
> array([[11, 12, 13, 14],
>        [21, 22, 23, 24],
>        [31, 32, 33, 34],
>        [41, 42, 43, 44]])
> 
> If I want to select the first two rows and first two columns, I do
> 
> >>> a[:2,:2]
> array([[11, 12],
>        [21, 22]])
> 
> If I want rows 1 and 2 or columns 1 and 2, it is easy and natural
> 
> >>> a[[1,2]]
> array([[21, 22, 23, 24],
>        [31, 32, 33, 34]])
> 
> >>> a[:,[1,2]]
> array([[12, 13],
>        [22, 23],
>        [32, 33],
>        [42, 43]])
> 
> but if I try to do both, I get the diagonal instead
> 
> >>> a[[1,2],[1,2]]
> array([22, 33])
> 
a[1:3,1:3]?

Can’t be generalised to arbitrary selections of rows,columns, though (e.g. a[1::2,::2] still works…)

				Derek




More information about the NumPy-Discussion mailing list