numpy array assignment

Scott Gilbert xscottgjunk at yahoo.com
Mon Sep 23 18:33:03 EDT 2002


John Hunter <jdhunter at ace.bsd.uchicago.edu> wrote:
>
> I have a 2 dimensional array X which is NxM and a 1 dimensional array
> v which is Nx1.  I want to assign v to the k-th column of X
> 
> import Numeric
> import RandomArray
> 
> Z = Numeric.zeros( (3,10), Numeric.Float  )
> v = Numeric.ones( (3,1), Numeric.Float )
> Z[:,4] = v
> 
> But I get the ValueError: Object too deep for desired array.  I have
> read over the numpy manual on slices and array methods and the closest
> thing I saw was the 'put' method, but this seems to be designed for 1d
> indexing.
> 
> What's the best way to assign an arbitrary Nx1 array to a column of X?
> 

Is this what you want?

     Z[:,4] = v[:,0]

v is actually a 2D array from Numeric's perspective, it's just the
second dimension isn't very deep.

Cheers,
    -Scott



More information about the Python-list mailing list