0

Let's say that I have a matrix with a given dimension, and also one array that contains a range of values. The values contained in the array are the indexes of the rows in which I want to extract the values out of my matrix. So, an example could be:

A = np.array([[3, 6, 7, 5, -3, 0], [5, -2, 2, 51, -13, 8], [13, -17, 18, 22, -12, 90], [33, -12, 12, 32, -42, 90]])

B = np.array([0, 2, 3])

the result expected:

array([[ 3,  7,  5],
       [ 5,  2, 51],
       [13, 18, 22],
       [33, 12, 32]])

fnaos
  • 151
  • 1
  • 12
  • I'm voting to close this, as it's a matter of basic NumPy functionality. This information is easily accessible in the numpy docs, and in at least one duplicate question, as such I can't see it being useful to any future readers. – AMC Mar 11 '20 at 21:55
  • Does this answer your question? [How to access the ith column of a NumPy multidimensional array?](https://stackoverflow.com/questions/4455076/how-to-access-the-ith-column-of-a-numpy-multidimensional-array) – AMC Mar 11 '20 at 21:56

1 Answers1

1

That's pretty simple in numpy- just do:

A[:, B]

Some useful ref: https://docs.scipy.org/doc/numpy/user/basics.indexing.html

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34