Numpy 将两个不同大小的向量相加
如果我有两个不同大小的 numpy 数组,如何将它们叠加。
a = numpy([0, 10, 20, 30])
b = numpy([20, 30, 40, 50, 60, 70])
将这两个向量相加以产生新向量(20、40、60、80、60、70)的最简洁方法是什么?
这是我的一般问题。作为背景,我专门应用了格林变换函数,并且需要将评估中每个时间步的结果叠加到之前累积的响应上。
If I have two numpy arrays of different sizes, how can I superimpose them.
a = numpy([0, 10, 20, 30])
b = numpy([20, 30, 40, 50, 60, 70])
What is the cleanest way to add these two vectors to produce a new vector (20, 40, 60, 80, 60, 70)?
This is my generic question. For background, I am specifically applying a Green's transform function and need to superimpose the results for each time step in the evaulation unto the responses previously accumulated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能就是您要寻找的内容,
基本上您复制较长的一个,然后就地添加较短的一个
This could be what you are looking for
basically you copy the longer one and then add in-place the shorter one
如果您知道
b
是更高维度,那么:就是您所需要的。
If you know that
b
is higher dimension, then:is all you need.
与上面的非常相似,但更紧凑一点:
Very similar to the one above, but a little more compact:
您所描述的是多项式加法,其实现为
numpy.polynomial.polynomial.polyadd
:Numpy 有一个完整的模块用于此类无限维向量运算:
numpy.polynomial
。只要确保不使用
numpy.polyadd
来自遗留numpy.poly1d
包,因为它们以相反的顺序处理系数的顺序。What you are describing is polynomial addition, that is implemented as
numpy.polynomial.polynomial.polyadd
:Numpy has a whole module for such infinite dimensional vector operations:
numpy.polynomial
.Just make sure to not use
numpy.polyadd
from the legacynumpy.poly1d
package, as they treat the order of coefficients in reversed order.