-2

Is there a way to mask, using np.ma module, all indices in a specific array smaller or bigger than a given number? For example, if I have an array of 365 elements and I want to mask all of the ones between 170 and 200 and only take into account[0:170] and [201:], can I do it?

Tried researching the answer but nothing I found seems like the right solution (it's not an issue for me to mask the indices using for example list comprehension, but I specifically need to use the np.ma module)

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
  • Don't use a list comprehension. Form a boolean array of zeros, slice-assign 1 over the indices you care about, and then use that as your mask argument. – Reinderien Oct 30 '22 at 18:25

1 Answers1

0

You could make a mask along the lines of

mymask = np.array([0 if x < 170 or x >=200 else 1 for x in range(365)])

and then use

x = np.ma.masked_array(myarray, mask = mymask)
user19077881
  • 3,643
  • 2
  • 3
  • 14
  • Alternative equivalent mask, which I think looks "cooler": 'mymask = np.array([1 if 170 <= x < 200 else 0 for x in range(365)])' – RufusVS Oct 30 '22 at 18:06