Numpy将两个不同大小的向量相加 [英] Numpy Adding two vectors with different sizes

查看:1127
本文介绍了Numpy将两个不同大小的向量相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个不同大小的numpy数组,如何叠加它们.

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])

将这两个向量相加以生成新向量(20、40、60、80、60、70)的最干净方法是什么?

What is the cleanest way to add these two vectors to produce a new vector (20, 40, 60, 80, 60, 70)?

这是我的一般性问题.对于背景,我专门应用了Green的变换函数,需要将评估中每个时间步的结果叠加到以前累积的响应上.

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.

推荐答案

这可能就是您要寻找的

if len(a) < len(b):
    c = b.copy()
    c[:len(a)] += a
else:
    c = a.copy()
    c[:len(b)] += b

基本上,您复制较长的一个,然后就地添加较短的一个

basically you copy the longer one and then add in-place the shorter one

这篇关于Numpy将两个不同大小的向量相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆