[Numpy-discussion] Slicing/selection in multiple dimensions simultaneously

Mike Ressler mike.ressler at alum.mit.edu
Tue Sep 11 18:11:53 EDT 2007


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.

Mike

-- 
mike.ressler at alum.mit.edu



More information about the NumPy-Discussion mailing list