简体   繁体   中英

Sum each row of a numpy array with all rows of second numpy array (python)

I would like to know if there is any fast way to sum each row of a first array with all rows of a second array. In this case both arrays have the same number of colulmns. For instance if array1.shape = (n,c) and array2.shape = (m,c) , the resulting array would be an array3.shape = ((n*m), c)

Look at the example below:

array1 = np.array([[0, 1, 2],
                   [3, 4, 5],
                   [6, 7, 8]])

array2 = np.array([[0, 1, 2],
                   [3, 4, 5]])

The result would be:

array3 = np.array([[0,  2,  4],
                   [3,  5,  7]
                   [3,  5,  7]
                   [6,  8, 10]
                   [6,  8, 10]
                   [9, 11, 13]])

The only way I see I can do this is to repeat each row of one of the arrays the number of rows of the other array. For instance, by doing np.repeat(array1, len(array2), axis=0) and then sum this array with array2 . This is not very practical however if the number of rows is too big. The other way would be with a for loop but this is too slow.

Any other better way to do it..?

Thanks in advance.

Extend array1 to 3D so that it becomes broadcastable against 2D array2 and then perform broadcasted addition and a final reshape is needed for desired output -

In [30]: (array1[:,None,:] + array2).reshape(-1,array1.shape[1])
Out[30]: 
array([[ 0,  2,  4],
       [ 3,  5,  7],
       [ 3,  5,  7],
       [ 6,  8, 10],
       [ 6,  8, 10],
       [ 9, 11, 13]])

You could try the following inline code if you haven't already. This is the simplest and probably also the quickest on a single thread.

>>> import numpy as np
>>> array1 = np.array([[0, 1, 2],
...                    [3, 4, 5],
...                    [6, 7, 8]])
>>> 
>>> array2 = np.array([[0, 1, 2],
...                    [3, 4, 5]])
>>> array3 = np.array([i+j for i in array1 for j in array2])
>>> array3
array([[ 0,  2,  4],
       [ 3,  5,  7],
       [ 3,  5,  7],
       [ 6,  8, 10],
       [ 6,  8, 10],
       [ 9, 11, 13]])
>>> 

If you are looking for speed up by treading, you could consider using CUDA ormultithreading . This suggestion goes a bit out of scope of your question but gives you an idea of what can be done to speed up matrix operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM