# [Numpy-discussion] How to sum weighted matrices

shu wei mailshuwei at gmail.com
Mon Mar 7 07:16:15 EST 2011

```Hello all,

I am new to python and numpy.
My question is how to sum up N weighted matrices.
For example w=[1,2] (N=2 case)
m1=[1 2 3,
3 4 5]

m2=[3 4 5,
4 5 6]
I want to get a matrix Y=w*m1+w*m2 by using a loop.

My original problem is like this
X=[1 2 3,
3 4 5,
4 5 6]

a1=[1 2 3]  1st row of X
m1=a1'*a1 a matirx
a2=[3 4 5] 2nd row of X
m2=a2'*a2
a3=[ 4 5 6] 3rd row of X
m3=a3'*a3

I want to get Y1=w*m1+w*m2
Y2=w*m2+w*m3
So basically it is rolling and to sum up the weighted matries
I have a big X, the rolling window is relatively small.

I tried to use

sq=np.array([x[i].reshape(-1,1)*x[i] for i in np.arange(0,len(x)]) #
s=len(x)
m=np.array([sq[i:i+t] for i in np.arange(0,s-t+1)]) # t is the len(w)

then I was stuck, I tried to use a loop somethig like
Y=np.array([np.sum(w[i]*m[j,i],axis=0) for i in np.arange(0,t)] )
Any suggestion is welcome.

sue
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110307/c2f54252/attachment.html>
```