https://uk.mathworks.com/help/images/ref/ordfilt2.html
ordfilt2 - 2-D order-statistic filtering - MATLAB
This MATLAB function replaces each element in A by the orderth element in the sorted set of neighbors specified by the nonzero elements in domain.
orderstatisticfilteringmatlab