To write a logical expression using boolean "or", one can use | symbol. Example, let's consider the following matrix:
\begin{equation}
M =
\left( \begin{array}{ccc}
1 & 0 & 0 \\
3 & 0 & 0 \\
1 & 0 & 0 \\
1 & 0 & 0 \\
2 & 0 & 0 \\
3 & 0 & 0 \\
1 & 0 & 0 \\
2 & 0 & 0
\end{array}\right)
\end{equation}
Now, to select the rows when the first columns is equal to 1 or 2, we can do:
>>> import numpy as np
>>> M = np.array([[1,0,0],[3,0,0],[1,0,0],[1,0,0],[2,0,0],[3,0,0],[1,0,0],[2,0,0]])
>>> M
array([[1, 0, 0],
[3, 0, 0],
[1, 0, 0],
[1, 0, 0],
[2, 0, 0],
[3, 0, 0],
[1, 0, 0],
[2, 0, 0]])
>>> N = M[ (M[:,0] == 1) | (M[:,0] == 2) ]
>>> N
array([[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[2, 0, 0],
[1, 0, 0],
[2, 0, 0]])
References
Link | Source |
---|---|
Multiple conditions using 'or' in numpy array | stackoverflow |