function [ax,mx,stdx] = mdauto(x,flag)
%MDAUTO Autoscales matrix with missing data to mean zero unit variance.
%  Autoscales matrix (x) with missing values indicated
%  by (flag), returning a matrix with unit variance columns (ax) 
%  and the vectors of means (mx) and standard deviations (stdx)
%  used in the scaling.
%
%  I/O format is: [ax,mx,stdx] = mdauto(x,flag);
%
%  See also: MDMNCN, MDSCALE

%  Copyright Eigenvector Research 1997
%  By Barry M. Wise

[m,n] = size(x);
mx = zeros(1,n);
stdx = zeros(1,n);
ax = ones(m,n)*flag;
for i = 1:n
  z = find(x(:,i)~=flag);
  mx(i) = mean(x(z,i));
  stdx(i) = std(x(z,i));
  ax(z,i) = (x(z,i)-mx(i))/stdx(i);
end
