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

    function d = constraintDistanceGrid(con, Xcell)
%CONSTRAINTDISTANCEGRID Evaluate constraint distance over a grid
%
%   D = CONSTRAINTDISTANCEGRID(CON, XCELL)
%
%   See also CONBASE/CONSTRAINTDISTANCE.

%   Copyright 2005-2007 The MathWorks, Inc.

% Number of input factors for constraint
NF = nFactors( con );

if length( Xcell ) ~= NF
    error(message('mbc:conbase:InvalidArgument2', NF));
end

% Number of points to evaluate
nPointsPerDim = cellfun( 'prodofsize', Xcell );
nPoints = prod( nPointsPerDim );

% So that we can easily handle very large grids, we are going to break down
% the evaluation into chunks. The "chunkSize" is the number of points we
% will handle at once.
chunkSize = min( 10000, nPoints );

% Allocate space for evaluation points and result
d     = zeros( nPoints,    1 );
Xeval = zeros( chunkSize, NF );

% Generate n-D grid for evaluation.
isNonSingletonDim = nPointsPerDim > 1;
nonSingletonDims = find(isNonSingletonDim);
if length(nonSingletonDims)>1
    % Form an ndgrid only if there is more than one non-singleton
    % dimension.  Gridding has no meaning with no vectors and has no effect
    % with one vector.
    [Xcell{nonSingletonDims}] = ndgrid( Xcell{nonSingletonDims} );
end

% Setup scalar inputs
for i = find( ~isNonSingletonDim )
    Xeval(:,i) = Xcell{i};
end

% Evaluate "chunkSize" points at a time
ai = 1; % start index
bi = 0; % end index
for i= 1:ceil( nPoints/chunkSize )
    thisChunkSize = min(chunkSize, nPoints-bi);
    bi = bi + thisChunkSize;
    
    % On the last time through this loop the chunk size may be reduced, so
    % we will have to reduce the Xeval matrix accordingly.  Doing this
    % every time through would force unnecessary copying of Xeval.
    if thisChunkSize<chunkSize
        Xeval = Xeval(1:thisChunkSize, :);
    end
    
    % Setup non-scalar inputs
    for j = nonSingletonDims 
        Xeval(:,j) = Xcell{j}(ai:bi);
    end
    
    d(ai:bi) = constraintDistance( con, Xeval );
    ai = ai + thisChunkSize;
end

% Make d the correct shape for a grid
if NF > 1
    d = reshape( d, nPointsPerDim );
end