[Numpy-discussion] Logical indexing and higher-dimensional arrays.

Sturla Molden sturla at molden.no
Tue Feb 7 13:41:41 EST 2012


On 07.02.2012 19:24, Sturla Molden wrote:
> On 07.02.2012 19:17, Benjamin Root wrote:
>
>>   >>>  print x.shape
>> (2, 3, 4)
>>   >>>  print x[0, :, :].shape
>> (3, 4)
>>   >>>  print x[0, :, idx].shape
>> (2, 3)
>
> That looks like a bug to me. The length of the first dimension should be
> the same.

I can reproduce this as well:

 >>> a = np.zeros((4,4,4))
 >>> b = np.array([0,1,1,1], dtype=bool)
 >>> a[0,:,:].shape
(4L, 4L)
 >>> a[0,:,b].shape
(3L, 4L)
 >>> i, = np.where(b)
 >>> a[0,:,i].shape
(3L, 4L)

Take a look at this:

 >>> a = np.zeros((1,2,3,4,5,6,7,8,9,10))
 >>> b = np.zeros(5, dtype=bool)

 >>> a[:,:,:,:,b,:,:,:,:,:].shape
(1L, 2L, 3L, 4L, 0L, 6L, 7L, 8L, 9L, 10L)

 >>> a[0,:,:,:,b,:,:,:,:,:].shape
(0L, 2L, 3L, 4L, 6L, 7L, 8L, 9L, 10L)

 >>> a[:,0,:,:,b,:,:,:,:,:].shape
(0L, 1L, 3L, 4L, 6L, 7L, 8L, 9L, 10L)

It's the combination of a single index and fancy indexing that does 
this, not the slicing.


Sturla




More information about the NumPy-Discussion mailing list