[Numpy-discussion] How to preserve number of array dimensions when taking a slice?

David Warde-Farley dwf at cs.toronto.edu
Sat Aug 8 04:46:08 EDT 2009


On 8-Aug-09, at 12:53 AM, Dr. Phillip M. Feldman wrote:

>
> I'd like to be able to make a slice of a 3-dimensional array, doing  
> something
> like the following:
>
> Y= X[A, B, C]
>
> where A, B, and C are lists of indices. This works, but has an  
> unexpected
> side-effect. When A, B, or C is a length-1 list, Y has fewer  
> dimensions than
> X. Is there a way to do the slice such that the number of dimensions  
> is
> preserved, i.e., I'd like Y to be a 3-dimensional array, even if one  
> or more
> dimensions is unity.  Is there a way to do this?

Err, X[A, B, C] with A, B and C lists should always return a 1D array,  
I think.

Lists of indices count as 'fancy indexing', not slicing. If using  
slices, you can specify slices that are only 1 long as in X[5:6, :, :]  
and retain the dimensionality.




More information about the NumPy-Discussion mailing list