[Numpy-discussion] indexing problem

Robert Kern robert.kern@gmail....
Tue Aug 18 11:27:21 CDT 2009


2009/8/18 Ernest Adrogué <eadrogue@gmx.net>:
> 18/08/09 @ 07:33 (-0700), thus spake Robert Kern:
>> 2009/8/18 Ernest Adrogué <eadrogue@gmx.net>:
>> > Hi,
>> >
>> > Suppose I have a 3-dimansional array, where one dimension
>> > is time. I'm not particularly interested in selecting specific
>> > moments in time, so most of the time I won't be indexing this
>> > dimension.
>> >
>> > Intuitively, one would make time the third dimension, but if
>> > you do that you have to specifiy the time in every index, which
>> > is annoying, because then all indices must start with an empty
>> > slice, e.g.,
>> >
>> > a[:,1]
>> > a[:,:,3]
>> > a[:,0,1]
>> >
>> > etc. On the other hand, the arrays resulting from indexing have
>> > all elements sorted by time, which is a good thing.
>> >
>> > Then, if I change it and make time the first dimension, it's
>> > handy because I can omit time in indices, BUT then the sub-arrays
>> > produced by indexing are not sorted by time!
>>
>> I do not know what you mean by "not sorted by time". You can keep the
>> sub-arrays sorted however you like regardless of the index used for
>> time. Can you show us an example of the problem you are seeing?
>
> Sorry for not explaining myself clearly enough.
>
> I'm using a masked arrays, and I call the compressed() method on
> the sub-arrays resulting from indexing, which gives a 1-d array. Is
> this 1-d array that isn't sorted the way I'd like.
>
> I'll try to explain this with an example. Here is a 3-d array
> that represents a 2-d array in differents moments in time (for
> ilustration purposes all elements in the 2-d array increase by
> one at each time point:
>
> In [35]: x=np.zeros((3,2,2))
>
> In [36]: x[0]=1
>
> In [37]: x[1]=2
>
> In [38]: x[2]=3
>
> In [39]: x
> Out[39]:
> array([[[ 1.,  1.],
>        [ 1.,  1.]],
>
>       [[ 2.,  2.],
>        [ 2.,  2.]],
>
>       [[ 3.,  3.],
>        [ 3.,  3.]]])
>
> Then if I take the elements [:,0] and flatten the resulting
> array, we can see that the resulting array has its elements sorted
> by time:
>
> In [40]: x[:,0].flatten()
> Out[40]: array([ 1.,  1.,  2.,  2.,  3.,  3.])
>
> But then I thought that it would be nice to arrange the data
> differently, so that the dimension that represents time can be
> omitted in the index.
>
> Therefore, I re-arrange the data in this way:
>
> In [41]: x=np.zeros((2,2,3))
>
> In [42]: x[:,:,0]=1
>
> In [43]: x[:,:,1]=2
>
> In [44]: x[:,:,2]=3
>
> In [46]: x
> Out[46]:
> array([[[ 1.,  2.,  3.],
>        [ 1.,  2.,  3.]],
>
>       [[ 1.,  2.,  3.],
>        [ 1.,  2.,  3.]]])
>
> But then, the flattened arrays I get are no longer in
> time-ascending order:
>
> In [45]: x[0].flatten()
> Out[45]: array([ 1.,  2.,  3.,  1.,  2.,  3.])
>
> It's a bit difficult to explain, but I hope it's more clear now!

x[0].T.flatten()

-- 
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


More information about the NumPy-Discussion mailing list