Skip to content
Advertisement

Combination of rows in numpy.ndarray

I have the following numpy.ndarray

JavaScript

I want to find all the possible combinations of sum of each row (sum of individual elements of a row except the last column) of S[0,:,:] with each row of S[1,:,:], i.e., my desired result is (order does not matter):

JavaScript

which is a 9-by-2 array resulting from 9 possible combinations of S[0,:,:] and S[1,:,:]. Although I have used a particular shape of S here, the shape may vary, i.e., for

JavaScript

in the above problem, x=2, y=3, and z=3, but these values may vary. Therefore, I am seeking for a generalized version.

Your help will be highly appreciated. Thank you for your time!

(Please no for loops if possible. It is pretty trivial then.)

Advertisement

Answer

You can use broadcast like this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement