How to preserve number of array dimensions when taking a slice?
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? -- View this message in context: http://www.nabble.com/How-to-preserve-number-of-array-dimensions-when-taking... Sent from the Numpy-discussion mailing list archive at Nabble.com.
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.
On Fri, Aug 7, 2009 at 23:53, Dr. Phillip M. Feldman<pfeldman@verizon.net> 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?
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (3)
-
David Warde-Farley
-
Dr. Phillip M. Feldman
-
Robert Kern