[Numpy-discussion] Array of Arrays

Pauli Virtanen pav at iki.fi
Fri Mar 23 16:38:14 EDT 2007


Alexander Michael kirjoitti:
> On 3/23/07, Nadav Horesh <nadavh at visionsense.com> wrote:
>   
>> How about
>>
>>  a = empty((5,7,4))
>>  c = a[...,-1]
>>     
>
> Solely because I want to use the array with code that assumes it is
> working with two-dimensional arrays but yet only performs operations
> on the "outer" two-dimensional array that would be consistent with an
> "inner" array type (i.e. scalar assignment, element-wise
> multiplication, etc.). I own all the code, so perhaps I can replace
> a[mask,-1] with a[mask,-1,...] and such. Hmm. Not bad reminder,
> thanks.
>   

In some cases the 2d code works without modifications also for 
3d-arrays, because in numpy

    a[1,2]

is the same as

    a[1,2,...]

for a 3d-array.

For example, the examples in your first post run as intended:

 >>> from numpy import *
 >>> a = empty((5,7,4), dtype=float64)
 >>> c = a[:,-1] # shape (5,4) array, i.e. last column of 4 element arrays
 >>> a[0,0] = 2.0
 >>> print a[0,0]
[ 2.  2.  2.  2.]
 >>> a[1,0] = 3.0
 >>> a[0,1] = a[0,0] * a[1,0]
 >>> print a[0,1]
[ 6.  6.  6.  6.]

-- 
Pauli Virtanen



More information about the NumPy-Discussion mailing list