function [fit,vaf,rowperm,colperm,addcon,coord] = linfittmac_altcomp(proxtm,inperm)


%  LINFITTMAC_ALTCOMP does a confirmatory two-mode fitting of a given 
%  unidimensional ordering of the row and column objects of
%  a two-mode proximity matrix PROXTM using Dykstra's (Kaczmarz's)
%  iterative projection least-squares method;
%  it differs from linfittm.m  by including the estimation of an
%  additive constant.
%
%  syntax: [fit,vaf,rowperm,colperm,addcon,coord] = ...
%    linfittmac_altcomp(proxtm,inperm)
%
%  INPERM is the given ordering of the row and column objects
%  together; FIT is an nrow (number of rows) by ncol (number
%  of columns) matrix  of absolute coordinate differences that
%  is fitted to PROXTM(ROWPERM,COLPERM) with VAF being the
%  variance-accounted-for.  ROWPERM and COLPERM are the row and
%  column object orderings derived from INPERM.  ADDCON is the
%  estimated additive constant that can be interpreted as being
%  added to PROXTM (or, alternatively, subtracted
%  from the fitted matrix FIT).  The nrow + ncol coordinates
%  (ordered with the smallest
%  set at a value of zero) are given in COORD.  Note that in comparison
%  with linfittmac.m, ADDCON here has the opposite sign.

[nrow,ncol] = size(proxtm);

n = nrow + ncol;

[member] = linear_order_member(inperm);

[fit,vaf,weights,addcon,end_condition] = partitionfit_twomode_addcon(proxtm,[member]);


coord = zeros(n,1);

for i = 1:(n-1)
    
    coord(i+1) = coord(i) + weights(i);
end

meancoord = sum(coord)/n;

for i = 1:n
    
    coord(i) = coord(i) - meancoord;
    
end

rowperm = zeros(nrow,1);

colperm = zeros(ncol,1);

rowidx = 0;
colidx = 0;

for i = 1:n
    
    if(inperm(i) <= nrow)
        
        rowidx = rowidx + 1;
        
        rowperm(rowidx) = inperm(i);
        
    else
        
        colidx = colidx + 1;
        colperm(colidx) = inperm(i) - nrow;
    end
end



