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

    function [Ar, Ac] = cg2dtablegradoperator(bp1, bp2)
%CG2DTABLEGRADOPERATOR Generate gradient operator for 2d tables
%
%   [AR, AC] = CG2DTABLEGRADOPERATOR(ROWBREAKS, COLBREAKS) generates
%   gradient operators for two dimensional tables. The table is specified
%   by the row and column breakpoints. AR is a gradient operator for the
%   row gradients and AC an operator for the column gradients.
%
%   See also CGLOOKUPTWO/GRADIENT, CGNORMFUNCTION/GRADIENT,
%   CGTABGRADCONSTRAINT/PRIVATE/PGEND

%   Copyright 2006 The MathWorks, Inc.

m= length(bp1);
n= length(bp2);

d1= diff(bp1);
d2= diff(bp2);

% rows
Ar= zeros((m-1)*n,m*n);
r=1;
for j=1:n
    for i= 2:m
        c= i + (j-1)*m;
        % vertical change (not for first row)
        Ar(r,c-1:c)   = [-1 1]/d1(i-1);
        r=r+1;
    end
end

% columns
Ac= zeros((n-1)*m,m*n);
r=1;
for j=2:n
    for i= 1:m
        c= i + (j-1)*m;
        % horizontal change (not for first column)
        Ac(r,[c-m c])   = [-1 1]/d2(j-1);
        r=r+1;
    end
end