Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mask Indices in an array

I have an array like:

data=np.array([1,2,3,5,8,7,2,1,3,5,1,2,20])

I would like to mask an array indices with a step of 3.
For an example I able to mask where the value of an array equal to 3.

import numpy as np
import numpy.ma as ma
x = np.array([1,2,3,5,8,7,2,1,3,5,1,2,20])
mx=ma.masked_values(x,3)
output:
[1 2 -- 5 8 7 2 1 -- 5 1 2 20]

Requirement:
I need to mask the every 3nd Indices in an array.(step of 3)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Required Output:

[1,2,3,--,8,7,--,1,3,--,1,2,--]

>Solution :

Let us create a mask based on the indices of the array:

np.ma.masked_array(x, np.r_[1, 1:len(x)] % 3 == 0)

masked_array(data=[1, 2, 3, --, 8, 7, --, 1, 3, --, 1, 2, --],
             mask=[False, False, False,  True, False, False,  True, False,
                   False,  True, False, False,  True],
       fill_value=999999)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading