3

I want to make all possible combinations of numpy arrays of different sizes into a bigger matrix. For examples a = np.array([1, 2, 3, 4, 5]) , b = np.array([6, 7, 8]), c = np.array([9, 10, 3, 4, 5]) the output should be:

 array([[1., 6., 9.],
       [2., 7., 10.],
       [3., 8., 3.],
       [4., 6., 4.],
       [5., 7., 5.],
       [1., 8., 10.],
       [2., 6., 3.],
       [3., 7., 4.],
       [4., 8., 5.],
       [5., 6., 9.],
       .....
       [5., 8., 5.])

to cover all possible combinations. Note that array b values are being repeated. I have tried making array of ones and then use broadcasting principle.

arr= np.ones((75,3))
arr[:,0]=arr[:,0]*a
arr[:,1]=arr[:,1]*b
arr[:,2]=arr[:,2]*c

But getting operands could not be broadcast together with shapes.

(Edit) need a solution that can have dynamic number of arrays of variable length. Not necessarily for three arrays.

mmcmp
  • 33
  • 4
  • is it always three arrays? I also think you mean to have every combinations of elements of three arrays in rows of output, which is different than what you printed as your desired output (to make it clear, think of three arrays of same sizes) – Ehsan Mar 10 '21 at 10:21
  • Number of arrays will be variable. I provided three as an example. The output should be all the possible combinations that can be made using the array elements which be the total of count of elements of each array multiplied. – mmcmp Mar 10 '21 at 10:34
  • Does this answer your question? [Using numpy to build an array of all combinations of two arrays](https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays) - has many answers for >2 arrays. – Daniel F Mar 10 '21 at 11:54
  • This answer covers a more general case. – mmcmp Nov 07 '21 at 19:25

2 Answers2

2

I think you are looking for meshgrid, which works for ANY number of arrays now by simply adding them to arguments:

np.array(np.meshgrid(a,b,c)).T.reshape(-1,3)

And if you have a list of arrays:

l = [a,b,c]
np.array(np.meshgrid(*l)).T.reshape(-1,len(l))

output:

array([[ 1,  6,  9],
       [ 1,  7,  9],
       [ 1,  8,  9],
       [ 2,  6,  9],
       [ 2,  7,  9],
       [ 2,  8,  9],
       [ 3,  6,  9],
       [ 3,  7,  9],
       [ 3,  8,  9],
       [ 4,  6,  9],
       [ 4,  7,  9],
       [ 4,  8,  9],
       [ 5,  6,  9],
       [ 5,  7,  9],
       [ 5,  8,  9],
       [ 1,  6, 10],
       [ 1,  7, 10],
       [ 1,  8, 10],
       [ 2,  6, 10],
       [ 2,  7, 10],
       [ 2,  8, 10],
       [ 3,  6, 10],
       [ 3,  7, 10],
       [ 3,  8, 10],
       [ 4,  6, 10],
       [ 4,  7, 10],
       [ 4,  8, 10],
       [ 5,  6, 10],
       [ 5,  7, 10],
       [ 5,  8, 10],
       [ 1,  6,  3],
       [ 1,  7,  3],
       [ 1,  8,  3],
       [ 2,  6,  3],
       [ 2,  7,  3],
       [ 2,  8,  3],
       [ 3,  6,  3],
       [ 3,  7,  3],
       [ 3,  8,  3],
       [ 4,  6,  3],
       [ 4,  7,  3],
       [ 4,  8,  3],
       [ 5,  6,  3],
       [ 5,  7,  3],
       [ 5,  8,  3],
       [ 1,  6,  4],
       [ 1,  7,  4],
       [ 1,  8,  4],
       [ 2,  6,  4],
       [ 2,  7,  4],
       [ 2,  8,  4],
       [ 3,  6,  4],
       [ 3,  7,  4],
       [ 3,  8,  4],
       [ 4,  6,  4],
       [ 4,  7,  4],
       [ 4,  8,  4],
       [ 5,  6,  4],
       [ 5,  7,  4],
       [ 5,  8,  4],
       [ 1,  6,  5],
       [ 1,  7,  5],
       [ 1,  8,  5],
       [ 2,  6,  5],
       [ 2,  7,  5],
       [ 2,  8,  5],
       [ 3,  6,  5],
       [ 3,  7,  5],
       [ 3,  8,  5],
       [ 4,  6,  5],
       [ 4,  7,  5],
       [ 4,  8,  5],
       [ 5,  6,  5],
       [ 5,  7,  5],
       [ 5,  8,  5]])
Ehsan
  • 12,072
  • 2
  • 20
  • 33
  • 1
    Thanks, How can I expend this to work for more case which I don't know how many input arrays I have beforehand? – mmcmp Mar 10 '21 at 10:43
1

You can use list comprehension:

arr = np.array([[x,y,z] for x  in a for y in b for z in c])
jlesuffleur
  • 1,113
  • 1
  • 7
  • 19
  • 1
    This is a good solution for the example, but I need to work in general case which I do not know how many arrays I will have. – mmcmp Mar 10 '21 at 10:45