function ipr = rgbipr_remove(ipr, tol)
	% ipr = rgbipr_merge(re_part, D_part, P_part tol)
	% merge particles which are too close together (below tol) 
	
    %% find matches which are close by
    % build adjacency matrix
    n_part      = size(ipr.re_part, 2);
    matches     = rangesearch(ipr.re_part.', ipr.re_part.', tol);
    n_matches   = cellfun(@(x) length(x), matches);
    rows        = repelem(1 : n_part, n_matches);
    cols        = cell2mat(matches(:).');
    
    %% the particle that is retained is brighter than all its neighbours
	D2mat		= sparse(rows, cols, ipr.d_part(cols).^2, n_part, n_part);
	[~,i_best]	= max(D2mat, [], 2);
	b_best		= i_best == (1:n_part)'; % flag indicating if this particle is best amongst neighbours
    
    ipr         = rgbipr_select(ipr, b_best);
end
