function Xs=standardize(X,d)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     function Xs=standardize(X,d)
%
%  substracts mean and divide matrix X by its std, 
%      both computed along dimension 1 (rows). 
%  
%  If Xyour matrix has more than 2 dims, it can always be reshaped 
%  that way without loss of generality.
% 
% Note : Ignores NaNs (uses nanmean and nanstd)
%     
%   Hepta Technologies, 2007
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[nrows,ncols]=size(X);

if ncols==1  % if only 1 timseries
	Xs=(X-nmean(X))./nstd(X);
else
means=nmean(X); % 
stdevs=nstd(X);
X=X-repmat(means,nrows,1);
Xs=X./repmat(stdevs,nrows,1);	
end

   

