Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Adding two vectors with different sizes

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.

like image 443
tnt Avatar asked Oct 25 '11 15:10

tnt


People also ask

How do I combine two NumPy arrays with different dimensions?

You can either reshape it array_2. reshape(-1,1) , or add a new axis array_2[:,np. newaxis] to make it 2 dimensional before concatenation.

How do I add two vectors to NumPy?

Use the numpy. add() Function to Perform Vector Addition in NumPy. The add() function from the numpy module can be used to add two arrays. It performs addition over arrays that have the same size with elements at every corresponding position getting summed up.

Can NumPy arrays have more than 2 dimensions?

Creating arrays with more than one dimensionIn general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.

How do I concatenate a different size vector in Python?

To concatenate the array of two different dimensions. The np. column_stack((array1, array2)) is used. To get the output, I have used print(array1).


1 Answers

This could be what you are looking for

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

like image 200
6502 Avatar answered Oct 14 '22 08:10

6502