www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregmodswitch/getClosestSwitchPoint.m

    function X = getClosestSwitchPoint(m, X)
%GETCLOSESTSWITCHPOINT Return the closest valid evaluation point
%
%  XVALID = GETCLOSESTSWITCHPOINT(M, X) returns the evaluation point that
%  is closest to the input X.

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


nf = nfactors(m);
if size(X, 2)~=nf
    error(message('mbc:xregmodswitch:InvalidArgument'));
end

[nPoints,ng] = size(m.OpPoints);
swFact = getSwitchFactors(m);
Xswitch = X(:, swFact);
if nPoints>0
    % Use coded inputs to find closest point - ensures that a single variable
    % does not dominate.  A temporary model is used to perform the coding.


    Inputs = mbcinputfactor(ng);
    Inputs = setcode(Inputs, m.ranges(:,end-ng+1:end)' );
    mCode = xregCreateModel(@xregmodel,Inputs);

    Xswitch = code(mCode, Xswitch);
    Xsites = code(mCode, m.OpPoints);

    for n = 1:size(Xswitch,1)
        if ~any(isnan(Xswitch(n,:)))
            codedDelta = repmat(Xswitch(n,:), nPoints , 1) - Xsites;
            [dist, idx] = min(sum(codedDelta.^2, 2));
            X(n, swFact) = m.OpPoints(idx, :);
        end
    end
end