Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-14 15:07:58 +01:00 committed by GitHub
parent cc7956cd71
commit cc90c61abb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,3 +25,92 @@ numpy.where(condition, [x, y, ]/)
> **x**, **y** : array_like
> Values from which to choose. x, y and condition need to be broadcastable to some shape.
## Finding indices
### 2d example
```python
import numpy as np
a = np.arange(0, 15).reshape((5, 3))
print(a)
print()
w = np.where(a > 7)
print(w)
```
Output:
```python
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
(array([2, 3, 3, 3, 4, 4, 4]), array([2, 0, 1, 2, 0, 1, 2]))
```
### 3d example
```python
import numpy as np
a = np.arange(0, 30).reshape((5, 3, 2))
w = np.where(a > 15)
print(w)
```
Output:
```python
(array([2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4]), array([2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2]), array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]))
```
Using the found indices:
```python
import numpy as np
a = np.arange(0, 30).reshape((5, 3, 2))
idx = np.where(a > 15)
a[idx] = 42
print(a)
```
Output:
```python
[[[ 0 1]
[ 2 3]
[ 4 5]]
[[ 6 7]
[ 8 9]
[10 11]]
[[12 13]
[14 15]
[42 42]]
[[42 42]
[42 42]
[42 42]]
[[42 42]
[42 42]
[42 42]]]
```
```python
import numpy as np
a = np.arange(0, 30).reshape((5, 3, 2))
a[np.where(a > 15)] = 42
print(a)
```