Sliding max over dimension

I’ve got a tensor where I want to make sure that all values in a certain axis are ascending. My idea was to slide a max over the dimension. Simple example:

[1,2,3,4,3,6]

after correction:

[1,2,3,4,4,6]

ideally, I would fold the max over a dimension using an accumulator (initialized with minus inf), but I wonder how to implement this in torch?

I think x.gather(dim, x.argsort(dim)) solves your problem.

import torch
x = torch.Tensor([[3, 1], [2, 4]])
print(x.gather(0, x.argsort(0))) # Columns sorted
print(x.gather(1, x.argsort(1))) # Rows sorted

Hi Leander,

#20240 has a race who get’s to implement cummax.

Best regards

Thomas

Sorry I didn’t get your point the first time I read :man_facepalming:!