0

I have a 1D numpy array and I need to create a new numpy array with the amax results (better the argmax result) of every 'n' positions of the array.

Let me try an example:

Consider I have a 1D numpy array with values from 1 to 5. I want to have a new numpy array with the amax, or argmax, of each 3 positions in the original numpy array.

original numpy array and range of amax action:

                  | 1 | 2 | 3 | 4 | 5 |
                  ------|         5 |--
                  3 |-------|
                      4 |-------|
                          5 |-------|
                  --|         5 |-----

My new numpy array, considering the amax of each 3 positions will be ([5, 3, 4, 5, 5]).

Can anyone think of a way to doing that just using numpy?

I was able to do it using a 'for', but I don't think that is the best option. Here is a snippet of what I did using 'for':

    for i in range(array-1):
        best = np.argmax([array[i-1], array[i], array[i+1,2]])
        best_array[i] = array[i+best-1]
Will R.
  • 1
  • 1
  • See if this works out for you - https://stackoverflow.com/questions/43288542/. – Divakar Oct 25 '17 at 15:55
  • The link above helps, but I was expecting a solution just using numpy. I want to avoid other libs. So, considering there is already an answer for what I need, this can be closed as duplicate. – Will R. Oct 25 '17 at 16:27
  • `strided_app(array, 3, 1 ).max(1)` with `strided_app` from - https://stackoverflow.com/a/40085052/. – Divakar Oct 25 '17 at 16:41

0 Answers0