% FUNCTION Y = EROSION(X, N_TIMES) function y = erosion(x, n_times) if n_times < 0 error( 'erosion needs n_times >= 0' ); end siz = size( x ); if sum( siz>1 ) > 1 disp( 'erosion: WARNING! Input has more than one dimension.' ); end if n_times < 1 y = x; return; end x = x(:); % Mirror-replicate beginning and end of data vector n_left = min( length( x ), n_times ); fill_left = x( n_left:-1:1 ); fill_left = [ repmat( fill_left( 1 ), n_times-n_left, 1 ); fill_left ]; n_right = min( length( x ), n_times ); fill_right = x( end:-1:end-n_right+1 ); fill_right = [ fill_right; repmat( fill_right( end ), n_times-n_right, 1 ) ]; aux = [fill_left; x(:); fill_right]; for a = 1:n_times % These Inf values won't go in the result anyway aux = min( [ aux, [aux(2:end); -Inf], [-Inf; aux(1:(end-1))] ], [], 2 ); end y = aux((1+n_times):(end-n_times)); y = reshape( y, siz );