I have an MxN array called A which stores the data I want. I have another M x N2 array B which stores array indices, and N2<N. Each row of B stores the indices of the elements I want to get from A for that row. For example, the following code works for me:
A_reduced = np.zeros((M,N2))
for i in range(M):
A_reduced[i,:] = A[i,B[i,:]]
Are there any 'vectorized' ways to extract the desired elements from A based on B instead of looping through each row?
[0, 0, 0]and B is[1, 2]. Would you expect A_reduced to now be[0, 0]A_reduced[i,:] = [A[i,B[i,:]] for i in range(M)]. I think numpy has a vectorize class but that might overcomplicate: docs.scipy.org/doc/numpy/reference/generated/…