[SciPy-user] Efficiently scaling matrix rows
Christian Kristukat
ckkart at hoc.net
Tue Nov 29 09:33:48 EST 2005
Neilen Marais wrote:
> 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?
mat = mat*arr[:,NewAxis] should do the same.
Regards, Christian
More information about the SciPy-User
mailing list