function [c, r, iter, err] = knight_sk(B, maxIters, tol)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [c, r, iter, err] = knight_sk(B, maxIters, tol)
%
% implements P A Knights's presentation of Sinkhorn-Knopp algorithm
% for scaline A in 1-norm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% by Daniel Ruiz and Bora Ucar,
% references: A symmetry preserving algorithm for matrix scaling
% Daniel Ruiz and Bora Ucar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[ii,jj,vv] = find(B);
[n, n] = size(B);
A = sparse(ii,jj, abs(vv), n, n);
if(nnz(B) ~= nnz(A)),
error('lost some nonzeros');
end
r = ones(n,1);
res = 1;
iter = 0;
while ((res > tol) && (iter