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

    function g = constraintDistance(c, X)
%CONSTRAINDIST -- Return distance from constraints
%
%  G = CONSTRAINDIST(C, X)
%
%  See also CONCONVEXHULL.

%  Copyright 2005-2014 The MathWorks, Inc.

X = pFilterFactors( c, X );

if isempty( c.A ) || isempty( c.b ),
    % No constraint, everything is outside
    g = ones( size( X, 1 ), 1 );
else
    % Evaluate constraint, i.e., Ax <= b
    %
    % The obvious way to do this is:
    %     bb = repmat( c.b, 1, size( X, 1 ) );
    %     sf = repmat( c.ScaleFactor, 1, size( X, 1 ) );
    %     g = max( (c.A * X' - bb)./sf, [], 1 );
    %     g = g(:);
    % But this can involve quite a chunk of memeory. Thus we choose proceed
    % as follows:
    nX = size( X, 1 );
    g = zeros( nX, 1 );
    % deal with scale factor before main calculation
    sf = c.ScaleFactor;
    A  =  bsxfun( @rdivide,c.A,sf);
    b  =  c.b./sf;

    Xi = X';
    for i=1:nX
        g(i) = max( A * Xi(:,i) - b );
    end
    
end