[Numpy-discussion] Array slices and number of dimensions

Thomas Robitaille thomas.robitaille at gmail.com
Wed Sep 1 17:54:26 EDT 2010


Hi,

I'm trying to extract sub-sections of a multidimensional array while keeping the number of dimensions the same. If I just select a specific element along a given direction, then the number of dimensions goes down by one:

>>> import numpy as np
>>> a = np.zeros((10,10,10))
>>> a.shape
(10, 10, 10)
>>> a[0,:,:].shape
(10, 10)

This makes sense to me. If I want to retain the initial number of dimensions, I can do

>>> a[[0],:,:].shape
(1, 10, 10)

However, if I try and do this along two directions, I do get a reduction in the number of dimensions:

>>> a[[0],:,[5]].shape
(1, 10)

I'm wondering if this is normal, or is a bug? In fact, I can get what I want by doing:

>>> a[[0],:,:][:,:,[5]].shape
(1, 10, 1)

so I can get around the issue, but just wanted to check whether the issue with a[[0],:,[5]] is a bug?

Thanks,

Tom





More information about the NumPy-Discussion mailing list