Vermillion - 1 year ago 46

Python Question

Using numpy, I have a matrix called

`points`

`points`

=> matrix([[0, 2],

[0, 0],

[1, 3],

[4, 6],

[0, 7],

[0, 3]])

If I have the tuple

`(1, 3)`

`points`

I tried using np.where:

`np.where(points == (1, 3))`

=> (array([2, 2, 5]), array([0, 1, 1]))

What is the meaning of this output? Can it be used to find the row where

`(1, 3)`

Answer Source

You were just needed to look for `ALL matches`

along each row, like so -

```
np.where((a==(1,3)).all(axis=1))[0]
```

Steps involved using given sample -

```
In [17]: a # Input matrix
Out[17]:
matrix([[0, 2],
[0, 0],
[1, 3],
[4, 6],
[0, 7],
[0, 3]])
In [18]: (a==(1,3)) # Matrix of broadcasted matches
Out[18]:
matrix([[False, False],
[False, False],
[ True, True],
[False, False],
[False, False],
[False, True]], dtype=bool)
In [19]: (a==(1,3)).all(axis=1) # Look for ALL matches along each row
Out[19]:
matrix([[False],
[False],
[ True],
[False],
[False],
[False]], dtype=bool)
In [20]: np.where((a==(1,3)).all(1))[0] # Use np.where to get row indices
Out[20]: array([2])
```