www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/mbcUniqueFilters.m

    function [Xg,KeepFirst,KeepLast] = mbcUniqueFilters(Xg,tol)
%UNIQUEFILTERS filters to keep unique points
%
% [Xg,KeepFirst,KeepLast] = uniqueFilters(X,tol)

%  Copyright 2009 The MathWorks, Inc. and Ford Global Technologies, Inc.

% get global data
if nargin<2
    tol = 1e-3;
end

% Form logical matrix indicating nonunique points
[n,m] = size(Xg);
D = false(n);

if isscalar(tol)
   tol = repmat(tol,1,m); 
end

% Range
R = max(Xg,[],1) - min(Xg,[],1);

for i=1:n
    if ~any(D(i,:))
        D(i,i) = true;
        for j=i+1:n
            IsDuplicate = true;
            for k=1:m
                IsDuplicate = IsDuplicate && abs(Xg(i,k)-Xg(j,k)) < tol(k)*R(k);
            end
            D(i,j) = IsDuplicate;
            D(j,i) = IsDuplicate;
        end
    else
        % already found this operating point
        first = find(D(i,:),1,'first');
        D(i,:) = D(first,:);
    end
end

% keep first
Dfirst = D;
Dlast = D;
for i=1:n
    if any(D(i,:))
        ni = sum(D(i,:));
        if ni>1
            % replace with mean value
            Xg(D(i,:),:) = repmat( mean(Xg(D(i,:),:),1), ni,1);
        end
        % remove first element so first element is kept
        first = find(D(i,:),1,'first');
        Dfirst(i,first) = false;
        % remove last element so last element is kept
        last = find(D(i,:),1,'last');
        Dlast(i,last) = false;
    end
end
KeepFirst = ~any(Dfirst,1);
KeepLast = ~any(Dlast,1);