function [r,next_sing_value]=rank_of_Sigma(Sigma,threshold,fid);
next_sing_value=-1;
dim=min(size(Sigma));
if  Sigma(1,1)<1
    fact=1;
else
    fact=Sigma(1,1);
end;
tol=max(size(Sigma))*fact*threshold;
%disp('rank_of..');
if dim==1
    r=sum(Sigma > tol);
    %r=sum(Sigma > threshold);
else
    r=sum(diag(Sigma) > tol);
    %r=sum(diag(Sigma) > threshold);
end;
if r < dim
    rp1=r+1;
    next_sing_value==Sigma(rp1,rp1);   
    fprintf(fid,'%s \n',['rank_of_Sigma: next singular value <  max(size(Sigma))*norm*threshold(= ', num2str(tol), ') is ' num2str(Sigma(rp1,rp1))]);
else
    fprintf(fid,'%s \n',['rank_of_Sigma: smallest singular value is ' num2str(Sigma(r,r)) ' > max(size(Sigma))*norm*threshold (= ' num2str(tol) ')' ]);
end;