function [outperm,coord,diff,fit] = uniscallp(prox,inperm)

%UNISCALLP carries out a unidimensional scaling of a symmetric proximity
%  matrix using iterative linear programming.
%  PROX is the input proximity matrix (with a zero main diagonal and a 
%  dissimilarity interpretation);
%  INPERM is the input beginning permutation (a permuation of the first $n$ integers).
%  OUTPERM is the final permutation of PROX.
%  COORD is the set of coordinates of the unidimensional scaling 
%  in ascending order;
%  DIFF is the value of the l1 loss function for the
%  coordinates and object permutation; and FIT is the matrix of absolute
%  coordinate differences being fit to PROX(OUTPERM,OUTPERM).

tic;
n = size(prox,1);
outperm = inperm;

[fit begindex coord exitflag] = linfitl1(prox,inperm);

nchange = 1;

while (nchange == 1)
    nchange = 0;
    
     for k = 1:(n-1)
        for j = (k+1):n
           
        intrperm = outperm;
        intrperm(k) = outperm(j);
        intrperm(j) = outperm(k);
           
       
        [fit tryindex coord exitflag] = linfitl1(prox,intrperm);
        
        if ((tryindex < (begindex - 1.0e-008)) & (exitflag > 0))
            
        
            nchange = 1;
            begindex = tryindex;
            outperm = intrperm;
        end
    end
end
end 

[fit diff coord exitflag] = linfitl1(prox,outperm);
toc