function [W,H] = nmfsc( V, rdim, maxiter, sW, sH,fname )

% SYNTAX:
% INPUTS:
% V          - data matrix 
% rdim       - number of components 
% maxiter    - number of maximum iteration
% sW         - sparseness of W, in [0,1]. (give [] if no constraint)
% sH         - sparseness of H, in [0,1]. (give [] if no constraint)
% fname      - name of file to write results into

% Check that we have non-negative data
if min(V(:))<0, error('Negative values in data!'); end
    
% Data dimensions
vdim = size(V,1);
samples = size(V,2);
    
% Create initial matrices
W = abs(randn(vdim,rdim)); 
H = abs(randn(rdim,samples));

myeps=10^-9;
neg=1;
if ~isempty(sH),

	for j=1 : maxiter,
		A =W'*W +sH*eye(rdim);
		for i= 1:samples,
			b=W'*V(:,i);
			H(:,i)=A\b;
		end
		if neg==1,
			H=H.*(H>0);
		end
		W=W.*(V*H')./(W*(H*H')+myeps);
	end
end

if ~isempty(sW),

	for k=1 : maxiter,
		A1 =H*H' +sW*eye(rdim);
		for aa= 1:vdim,
			c=H*V(aa,:)';
			W(aa,:)=(A1\c)';
		end
		if neg==1,
		W=W.*(W>0);
		end
		H=H.*(W'*V)./(W'*(W*H)+myeps);
	end
end

