Suppose I have two numpy arrays A, B, with A.shape = (2,4,3) and B.shape = (2,4):
A =
[[[-1 -1 0]
[-1 0 0]
[-1 0 0]
[ 0 -1 0]]
[[-1 0 0]
[-1 0 0]
[-1 1 0]
[ 0 0 0]]]
B =
[[0 2 0 3]
[1 0 0 0]]
Now I would like to get a new array C with C.shape = (2+3+1,3) = (6,3), such that each integer in B specifies the number of the corresponding 3x1 array in A. In other words, A[i,j,:] should appear B[i,j] times in C. In our case,
C =
[[-1 0 0]
[-1 0 0]
[ 0 -1 0]
[ 0 -1 0]
[ 0 -1 0]
[-1 0 0]]
What is the fastest way to obtain C?