, ( ) .
MATLAB, , colfilt, .
output = colfilt(data, [5 5], 'sliding', @mode)
, padarray, 3 , , 3 .
% Pad with replicates of the data
data = padarray(data, [3 3], 'replicate', 'both');
% Perform the filtering
new = colfilt(data, [5 5], 'sliding', @mode);
% Crop out the padding part
new = new(4:end-3,4:end-3);
n, :
function out = mode_filter(data, n)
pad_size = ceil(n / 2);
% Pad with replicates of the data
data = padarray(data, [pad_size, pad_size], 'replicate', 'both');
% Perform the filtering
out = colfilt(data, [n n], 'sliding', @mode);
% Crop out the padded part
out = out((pad_size + 1):(end - pad_size), (pad_size + 1):(end - pad_size));
end