[[+content_image]]
D
D
Daniil Shevkunov2021-06-08 14:55:22
Python
Daniil Shevkunov, 2021-06-08 14:55:22

How to get specific rows in a 3 dimensional NumPy array?

I have a numpy array:
a = np.array([, ])

Another kind

([[[1, 0, 0],
[2, 0, 1],
[2, 0, 2]],

[[0, 0, 0],
[2, 0, 3],
[0, 0, 0]]])


i need to get all rows that start with 2 without a loop
i thought to do it something like this:
a[: , a[: , :][0] == 2]
But this returns this
([[2, 2, 2] ,
2, 0, 0]])

What am I doing wrong? How right?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
dmshar, 2021-06-08
@danila763

Maybe like this:

import numpy as np
a = np.array([[[1, 0, 0], [2, 0, 1], [2, 0, 2]], [[0, 0, 0], [2, 0, 3], [0, 0, 0]]])
a[a[:,:,0]==2]

Result:
array([[2, 0, 1],
       [2, 0, 2],
       [2, 0, 3]])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question