[SciPy-user] Efficiently scaling matrix rows

Neilen Marais nmarais at sun.ac.za
Tue Nov 29 08:00:26 CST 2005


Hi

I have a an n x 3 matrix (i.e. each row contains three elements). I want
to scale each row by the element of a 1-d array. I.o.w, if I have 

arr = array([1., 2., 3., ....]),

I want to scale the first row of the matrix by arr[0], the second by
arr[1], etc. ATM I'm doing this:

coefs = reshape(repeat(arr, 3), (-1, 3))
mat = coefs*mat

Is there a more efficent (and potentially easier to read) way that does
not require redundant copies of arr, while avoiding a slow python loop?

Thanks
Neilen



More information about the SciPy-user mailing list