www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cglookuptwo/deltablecon.m

    function [A,C,nzt]= deltablecon(LT,nzt,MaxChg)
% DELTABLECON linear constraint matrix for change in table entries
%
% [A,C]= deltablecon(m,n,MaxChg,nzt)
%  Inputs
%     MaxChg   Optional limit for constraint (scalar or vector)
%     nzt      Optionas row vector specifying selection of cells to constrain
%  Outputs
%     A,C      Linear constraints for fmincon  A*x < C

%  Copyright 2005-2012 The MathWorks, Inc.

if nargin<3
    MaxChg= MaxGradient(LT);
end

bp= get(LT,'axes');
bpy = bp{2};
bpx = bp{1};
[n,m] = size(get(LT,'Values'));

locks= get(LT,'vlocks');
if ~isempty(locks)
    IsLocked= locks(:)';
else
    IsLocked= false(1,m*n);
end
if nargin<2 || isempty(nzt)
    % default is to constrain non locked cells
    nzt = ~IsLocked;
end


dx= diff(bpy);
mx= max(dx);
dy= diff(bpx);
my= max(dy);

% calculate gradient matrix for each dimension
Adim = gradient(LT);

% build linear constraint matrix (scaled)
A= [-mx*Adim{1};mx*Adim{1} ; -my*Adim{2};my*Adim{2}];
Nr= size(Adim{1},1);
Nc= size(Adim{2},1);

% Gradient limits
MaxChg(1,:)= MaxChg(1,:)*mx;
MaxChg(2,:)= MaxChg(2,:)*my;

C= zeros(2*Nr+2*Nc ,1);
C(1:Nr)= -MaxChg(1,1);
C(Nr+1:2*Nr)= MaxChg(1,2);
C(2*Nr+1:2*Nr+Nc)= -MaxChg(2,1);
C(2*Nr+Nc+1:end)= MaxChg(2,2);

if isempty(nzt)
    nzt= true(1,m*n);
else
    % first find cells to remove from constraint
    ztpts= true(1,m*n);
    ztpts(nzt)= false;

    % delete zero table entries
    f=[];
    V= LT.Values(:);
    for i= find(ztpts)
        % find constraints which contain variable for cell i.
        ind= find(A(:,i));
        if IsLocked(i)
            % change variable to bound because this cell is fixed
            C(ind)= C(ind)-A(ind,i)*V(i);
            % remove variable
            A(ind,i)= 0;
        else
            % make bound infinite so it is removed
            C(ind)= Inf;
        end
    end

    % delete cell variables
    A(:,ztpts)=[];
    % delete constraints which do nothing
    f= ~any(A,2);
    A(f,:)= [];
    % delete bounds
    C(f,:)=[];
end


A= A(isfinite(C),:);
C= C(isfinite(C));