[Numpy-discussion] indexing problem
Tue Aug 18 11:22:28 CDT 2009
18/08/09 @ 07:33 (-0700), thus spake Robert Kern:
> 2009/8/18 Ernest Adrogué <firstname.lastname@example.org>:
> > 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 : x=np.zeros((3,2,2))
In : x=1
In : x=2
In : x=3
In : x
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
In : x[:,0].flatten()
Out: 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 : x=np.zeros((2,2,3))
In : x[:,:,0]=1
In : x[:,:,1]=2
In : x[:,:,2]=3
In : x
array([[[ 1., 2., 3.],
[ 1., 2., 3.]],
[[ 1., 2., 3.],
[ 1., 2., 3.]]])
But then, the flattened arrays I get are no longer in
In : x.flatten()
Out: array([ 1., 2., 3., 1., 2., 3.])
It's a bit difficult to explain, but I hope it's more clear now!
More information about the NumPy-Discussion