So I am trying to select some columns from a 3D matrix based on the values in a vector using Numpy. I have already solved the problem using a list comprehension, but I figured that there might be a better way using Numpy's builtin methods. Does anyone know if such a method or combination of methods exist?
matrix1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
matrix2 = np.array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
total_matrix = np.array([matrix1, matrix2])
vector = [0,1,1]
# Retrieve the first column from the first matrix, second and third from the second matrix.
result = np.array([total_matrix[index2,: , index1] for index1, index2 in enumerate(vector)]).transpose()
# result:
np.array([[1, 11, 12],
[4, 14, 15],
[7, 15, 18]])