[Numpy-discussion] Is this an indexing bug?

Sturla Molden sturla at molden.no
Wed Jun 20 15:35:19 EDT 2007

> These are both correct.  See my previous posts about the rule.
> The first case is exactly the example we saw before:  we start with a
> (1,10,10)-shaped array and replace the first and last-dimension
> (1,10)-shaped array with a (5,)-shaped array.  Not having a clear place
> to put the extracted (5,)-shaped subspace, it is tacked on to the front.
> In the second example, the last-dimension (10,)-shaped sub-space is
> replaced with a (5,)-shaped sub-space.  There is no ambiguity in this
> case and the result is a (1,10,5)-shaped array.
> There is no bug here.   Perhaps unexpected behavior with "advanced
> indexing" combined with single-integer indexing in separated dimensions
> but no bug.  The result, does follow an understandable and generalizable
> rule.


I agree that there is no bug here, as the software follows the specified
behaviour. But it may be debated whether the specified behaviour is
sensible or not.

I think the source of the confusion is the different behaviour of
'advanced indexing' in NumPy and Matlab. This is what Matlab does:

>> x = zeros(1,10,10);
>> size( x(1,:,[1 2 3 4 5]) )

ans =

     1    10     5

>> size( x(:,:,[1 2 3 4 5]) )

ans =

     1    10     5

I might be worth pointing this out in the NumPy documentation (on the
SciPy web site and in your book), so users don't expect similar behaviour
of 'advanced indexing' in NumPy and Matlab.

Sturla Molden

More information about the NumPy-Discussion mailing list