[SciPy-user] Efficiently scaling matrix rows
ckkart at hoc.net
Tue Nov 29 08:33:48 CST 2005
Neilen Marais wrote:
> 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, the second by
> arr, 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?
mat = mat*arr[:,NewAxis] should do the same.
More information about the SciPy-user