numpy array assignment

John Hunter jdhunter at
Mon Sep 23 11:13:49 EDT 2002

>>>>> "John" == John Hunter <jdhunter at> writes:

    John> I have a 2 dimensional array X which is NxM and a 1
    John> dimensional array v which is Nx1.  I want to assign v to the
    John> k-th column of X

This is what I've come up with so far -- if there is a better/faster
way, please let me know....

import Numeric

def putcol(k, Z, v):
    (n,m) = Z.shape
    Numeric.put(Z, k+m*Numeric.arange(n), v)

def putrow(k, Z, v):
    (n,m) = Z.shape
    Numeric.put(Z, k*m+Numeric.arange(m), v)

Z = Numeric.zeros( (3,5), Numeric.Float  )
vc = Numeric.ones( (3,1), Numeric.Float )
vr = Numeric.ones( (1,5), Numeric.Float )

putcol(3, Z, vc)
putrow(1, Z, vr)
print Z

More information about the Python-list mailing list