mmcmp
mmcmp

Reputation: 33

Combine numpy arrays of different sizes into a bigger matrix

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.

Upvotes: 3

Views: 703

Answers (2)

Ehsan
Ehsan

Reputation: 12397

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

Upvotes: 2

jlesuffleur
jlesuffleur

Reputation: 1253

You can use list comprehension:

arr = np.array([[x,y,z] for x  in a for y in b for z in c])

Upvotes: 1

Related Questions