function C = compar(A, k)
%COMPAR Comparison matrices.
%   GALLERY('COMPAR',A) is DIAG(B) - TRIL(B,-1) - TRIU(B,1), 
%   where B = ABS(A). GALLERY('COMPAR',A) is often denoted by M(A)
%   in the literature.
%
%   GALLERY('COMPAR',A,1) is a modified version of A. Each diagonal 
%   element is replaced by its absolute value. Each off-diagonal 
%   element A(M,N) is replaced by -MAX(ABS(A(M,:))).
%   However, if A is triangular, GALLERY('COMPAR',A,1) is too.
%
%   See also COMP.

%   Reference:
%   [1] N.J. Higham, A survey of condition number estimation for
%       triangular matrices, SIAM Review, 29 (1987), pp. 575-596.
%
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.6 $  $Date: 1997/11/21 23:29:26 $

if nargin == 1, k = 0; end
[m, n] = size(A);
p = min(m, n);

if k == 0

% This code uses less temporary storage than the `high level' definition above.
   C = -abs(A);
   for j=1:p
     C(j,j) = abs(A(j,j));
   end

elseif k == 1

   C = A';
   for j=1:p
       C(k,k) = 0;
   end
   mx = max(abs(C));
   C = -mx'*ones(1,n);
   for j=1:p
       C(j,j) = abs(A(j,j));
   end
   if all( A == tril(A) ), C = tril(C); end
   if all( A == triu(A) ), C = triu(C); end

end
