2

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 1

4

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]] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.