function S=normalizeRows(S,q) % function S=normalizeRows(S,q) % % Normalizes the rows of the (possibly sparse) matrix S. % The normalization is done with respect to L_q norm. % If a row is all-zero, it is left unchanged. % % -- Guy Lebanon, 2004 if nargin==1, q=1; end I=find(sum(S,2)==0); if issparse(S), if q~=1, sums=(sum(abs(S).^q,2)).^(1/q); else sums = sum(abs(S),2); end S = spdiags(1./sums,0,size(S,1),size(S,1)) * S; else if q~=1, sums=(sum(abs(S).^q,2)).^(1/q); else sums=sum(abs(S),2); end S=S./repmat(sums,[1 size(S,2)]); end S(I,:)=0;