Fran Ahedo Guerrero - 2 months ago 11

Python Question

I have this vector

`v = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20)`

I want to remove the multiples of 2 and 3. How would I do this? I tried to do this but I can not:

`v = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20)`

Mul3 = np.arange (1.21,3)

Mul2 = np.arange (1.21.2)

V1 = v [-mul2]

V2 = v1 [-mul3]

Answer Source

Given that you use NumPy already you can use boolean array indexing to remove the multiples of `2`

and `3`

:

```
>>> import numpy as np
>>> v = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,19,20) # that's a tuple
>>> arr = np.array(v) # convert the tuple to a numpy array
>>> arr[(arr % 2 != 0) & (arr % 3 != 0)]
array([ 1, 5, 7, 11, 13, 19])
```

The `(arr % 2 != 0)`

creates a boolean mask where multiples of `2`

are `False`

and everything else `True`

and likewise the `(arr % 3 != 0)`

works for multiples of `3`

. These two masks are combined using `&`

(and) and then used as mask for your `arr`