www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@conswitch/SnapTolerance.m

    function [m,KeepLast] = SnapTolerance(m,Tol,Indices)
%SNAPTOLERANCE snap operating points to tolerance
%
% [m,KeepLast] = SnapTolerance(m,Tol,Indices);
%   Operating Points within tolerance for each operating point variable are
%   set to the mean value for that group.

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


if nargin<3
   Indices = 1:size(m.OpPoints,2); 
end
Xall = m.OpPoints(:,Indices);
[N,nop]= size(Xall);

Xsort = zeros(size(Xall));
SortIndices = zeros(size(Xall));
for i=1:nop
  [Xsort(:,i),SortIndices(:,i)]= sort(Xall(:,i));
end

OriginalTolerance = m.Tolerance;

m.Tolerance = Tol;
abstol = getAbsoluteTolerance(m);
% abstol = [50;7.5];

for j=Indices
    i = 1;
    while i<N
        d = Xsort(:,j)-Xsort(i,j);
        % find points within tolerance
        samepoints= d>=0 & d < abstol(j);
        if sum(samepoints)>1
            % replace with average value
            Xsort(samepoints,j) = mean(Xsort(samepoints,j));
        end
        % next point to process
        i = find(samepoints,1,'last')+1;
    end
    % need unsorted operating points
    Xall(SortIndices(:,j),j) = Xsort(:,j);
end

% update operating points
if length(Indices)~=size(m.OpPoints,2)
    m.OpPoints(:,Indices) = Xall;
else
    m.OpPoints = Xall;
end

% make sure that operating points are unique
m.Tolerance = OriginalTolerance;
[~,~,KeepLast] = mbcUniqueFilters(m.OpPoints,OriginalTolerance);
m = selectModels(m,KeepLast);