Am I right to assume that there is no way elegant way to interact with slices. i.e. Is there anyway to get
to work? So that the dimension is completely specified? Or perhaps the only way to do this is via
If anyone knows a better way?
On Tue, Sep 11, 2007 at 6:13 PM, Travis E. Oliphant firstname.lastname@example.org wrote:
Timothy Hochberg wrote:
On 9/11/07, *Robert Kern* <email@example.com mailto:firstname.lastname@example.org> wrote:
Mike Ressler wrote: > The following seems to be a wart: is it expected? > > Set up a 10x10 array and some indexing arrays: > > a=arange(100) > a.shape=(10,10) > q=array([0,2,4,6,8]) > r=array([0,5]) > > Suppose I want to extract only the "even" numbered rows from a - then > > print a[q,:] > > <works - output deleted> > > Every fifth column: > > print a[:,r] > > <works - output deleted> > > Only the even rows of every fifth column: > > print a[q,r] > > ---------------------------------------------------------------------------
> <type 'exceptions.ValueError'> Traceback (most recent call last) > > /.../.../.../<ipython console> in <module>() > > <type 'exceptions.ValueError '>: shape mismatch: objects cannot be > broadcast to a single shape > > But, this works: > > print a[q,:][:,r] > > [[ 0 5] > [20 25] > [40 45] > [60 65] > [80 85]] > > So why does the a[q,r] form have problems? Thanks for your insights.
It is intended that the form a[q,r] be the general case: q and r are broadcasted against each other to a single shape. The result of the indexing is an array of that broadcasted shape with elements found by using each pair of elements in the broadcasted q and r arrays as indices.
There are operations you can express with this form that you couldn't if the behavior that you expected were the case whereas you can get the result you want relatively straightforwardly.
In : a[q[:,newaxis], r] Out: array([[ 0, 5], [20, 25], [40, 45], [60, 65], [80, 85]])
At the risk of making Robert grumpy: while it is true the form we ended up with is more general I've come to the conclusion that it was a bit of a mistake. In the spirit of making simple things simple and complex things possible, I suspect that having fancy-indexing do the obvious thing here and delegating the more powerful but also more difficult to understand case to a function or method would have been overall more useful. Cases where the multidimensional features of fancy-indexing get used are messy enough that they don't benefit much from the conciseness of the indexing notation, at least in my experience.
This is a reasonable argument. It is reasonable enough that I intentionally made an ix_ function to do what you want.
does as originally expected if a bit more line-noise.
Numpy-discussion mailing list Numpyemail@example.com http://projects.scipy.org/mailman/listinfo/numpy-discussion