This is almost the same as this question, but in numpy, and for matrices over some dimension:
I have two matrices of length n, A
and B
, for example,
n = 1000
A = np.random.rand(n, 3, 4, 5)
B = np.random.rand(n, 3, 4, 5)
I want a new matrix C
, in which every element (C[i]
) is a matrix of shape (2, 3, 4, 5)
: C[i][0]
is A[k]
, and C[i][0]
is B[k]
for some k
, where elements from A
and B
are selected once for every other element in the other.
A more concrete example:
A = [elem1, elem2, elem3]
B = [elem4, elem5, elem6]
then
C = [
[elem1, elem4],[elem1, elem5],[elem1, elem6],
[elem2, elem4],[elem2, elem5],[elem2, elem6],
[elem3, elem4],[elem3, elem5],[elem3, elem6]
]
I am aware I can use itertools to build this, but i figured it already exists, and faster.