I have a 2D numpy array A, and a list x. The elements of x are indices of the rows of A. I want to create a new matrix B, by taking the rows of A as indicated by x. How can I do this?
1 Answer
You can pass x as an argument when indexing A to create your new matrix B as below. See the docs here.
import numpy as np
A = np.arange(25).reshape((5,5))
x = [1, 2, 4]
B = A[x]
print(B)
# [[ 5 6 7 8 9]
# [10 11 12 13 14]
# [20 21 22 23 24]]