Reputation: 322
I'm trying want to fetch the rows that are having even numbers from the array below:
mat1 = np.array([[23,45,63],[22,78,43],[12,77,47],[53,47,33]]).reshape(4,3)
mat1
array([[23, 45, 63],
[22, 78, 43],
[12, 77, 47],
[53, 47, 33]])
And the below code returns only the values..
mat1[mat1%2==0]
array([22, 78, 12])
Is there any way to fetch the entire row/column having the even numbers?
Upvotes: 1
Views: 202
Reputation: 59731
You can do that like this:
import numpy as np
mat1 = np.array([[23,45,63],[22,78,43],[12,77,47],[53,47,33]])
is_even = (mat1 % 2 == 0)
# Rows
print(mat1[is_even.any(1)])
# [[22 78 43]
# [12 77 47]]
# Columns
print(mat1[:, is_even.any(0)])
# [[23 45]
# [22 78]
# [12 77]
# [53 47]]
Upvotes: 1