1

I have this 1d array which represents the ranges of valid values.

ranges = np.array([1, 2, 0])

and i have multiple 2d arrays that have values, which i want to mask by index based on the ranges above.

matrixes = np.array([[0, 1, 2],
                     [3, 4, 5],
                     [6, 7, 8]])

So each value in "ranges" represents the number of rows that have valid values in "matrixes" (the columns correspond to each-other), so the masking would return something like:

              ([[True,  True,  False],
                [False, True,  False],
                [False, False, False]])

The first column has 1 valid value, the first. The second column has 2 valid values... etc.

I tried several other topics but could not figure out how to do it... ex: How to properly mask a numpy 2D array?

Andrei M.
  • 313
  • 2
  • 10

1 Answers1

1

Use broadcasting:

out = np.arange(matrixes.shape[0])[:,None]<ranges 

Output:

[[ True  True False]
 [False  True False]
 [False False False]]
mozway
  • 194,879
  • 13
  • 39
  • 75
  • 1
    Hey, thanks for the reply. With some transposing it seems to work fine, although a bit one-line-ish :) Thank you. – Andrei M. Apr 19 '23 at 07:08