python - Modify numpy array column by column inside a loop -


is there way modify numpy array inside loop column column?

i expect done code that:

import numpy n  cna=n.array([[10,20]]).t mnx=n.array([[1,2],[3,4]]) cnx in n.nditer(mnx.t, <some params>):     cnx = cnx+cna 

which parameters should use obtain mnx=[[10,23],[12,24]]?

i aware problem solved using following code:

cna=n.array([10,20]) mnx=n.array([[1,2],[3,4]]) col in range(mnx.shape[1]):     mnx[:,col] = mnx[:,col]+cna 

hovewer, in python loop through modified objects, not indexes, question - possible loop through columns (that need modified in-place) directly?

just know, of us, in python, iterate on indices , not modified objects when helpful. although in numpy, general rule, don't explicitly iterate unless there no other way out: problem, simplest approach skip iteration , rely on broadcasting:

mnx += cna 

if insist on iterating, think simplest iterate on transposed array:

for col in mnx.t:     col += cna[:, 0].t 

Comments