Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

python - Sum of matrices using np.add()

I have a list of n matrices where n = 5:

[matrix([[3.62425112, 0.00953506],
        [0.00953506, 1.05054417]]), matrix([[4.15808905e+00, 9.27845937e-04],
        [9.27845937e-04, 9.88509628e-01]]), matrix([[3.90560856, 0.0504297 ],
        [0.0504297 , 0.92587046]]), matrix([[ 3.87347073, -0.12430547],
        [-0.12430547,  1.09071475]]), matrix([[ 3.87697392, -0.00475038],
        [-0.00475038,  1.01439917]])]

I want to do element-wise addition of these matrices:

I am trying this:

np.add(S_list[0], S_list[1], S_list[2], S_list[3], S_list[4])

It works but I don't want to fix n = 5

Can anyone please help? Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

by the documentation, np.add should add only two matrices.

However np.add.reduce(S_list) or just sum(S_list) will give you what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...