Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...