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?

1 Answer 1

1

Use broadcasting:

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

Output:

[[ True  True False]
 [False  True False]
 [False False False]]
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for the reply. With some transposing it seems to work fine, although a bit one-line-ish :) Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.