Skip to content
Advertisement

Matrix row masking at different indices

I have an 2-D array that I want to find the max value per row and then find the next max-value that is not within +/- n of the previous value. For example I have the following matrix:

JavaScript

Now I’d like to get the next max value not within say +/- 2 of the current max.

Here is what I have but it feels sloppy:

JavaScript

Any ideas on doing this faster through indexing/windowing?

Advertisement

Answer

You can create a mask around the indices you compute for the max by taking an array of indices and subtracting the relevant max_indices, and then use the masked api to recompute the argmax:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement