51 lines
1.8 KiB
Matlab
51 lines
1.8 KiB
Matlab
function [z,mu,sigma] = zscore(x,flag,dim)
|
|
%ZSCORE Standardized z score.
|
|
% Z = ZSCORE(X) returns a centered, scaled version of X, the same size as X.
|
|
% For vector input X, Z is the vector of z-scores (X-MEAN(X)) ./ STD(X). For
|
|
% matrix X, z-scores are computed using the mean and standard deviation
|
|
% along each column of X. For higher-dimensional arrays, z-scores are
|
|
% computed using the mean and standard deviation along the first
|
|
% non-singleton dimension.
|
|
%
|
|
% The columns of Z have sample mean zero and sample standard deviation one
|
|
% (unless a column of X is constant, in which case that column of Z is
|
|
% constant at 0).
|
|
%
|
|
% [Z,MU,SIGMA] = ZSCORE(X) also returns MEAN(X) in MU and STD(X) in SIGMA.
|
|
%
|
|
% [...] = ZSCORE(X,1) normalizes X using STD(X,1), i.e., by computing the
|
|
% standard deviation(s) using N rather than N-1, where N is the length of
|
|
% the dimension along which ZSCORE works. ZSCORE(X,0) is the same as
|
|
% ZSCORE(X).
|
|
%
|
|
% [...] = ZSCORE(X,FLAG,DIM) standardizes X by working along the dimension
|
|
% DIM of X. Pass in FLAG==0 to use the default normalization by N-1, or 1
|
|
% to use N.
|
|
%
|
|
% See also MEAN, STD.
|
|
|
|
% Copyright 1993-2006 The MathWorks, Inc.
|
|
% $Revision: 1.7.2.5 $ $Date: 2007/12/10 23:06:31 $
|
|
|
|
% [] is a special case for std and mean, just handle it out here.
|
|
if isequal(x,[]), z = []; return; end
|
|
|
|
if nargin < 2
|
|
flag = 0;
|
|
end
|
|
if nargin < 3
|
|
% Figure out which dimension to work along.
|
|
dim = find(size(x) ~= 1, 1);
|
|
if isempty(dim), dim = 1; end
|
|
end
|
|
|
|
% Compute X's mean and sd, and standardize it
|
|
mu = mean(x,dim);
|
|
sigma = std(x,flag,dim);
|
|
sigma0 = sigma;
|
|
sigma0(sigma0==0) = 1;
|
|
|
|
z = (x-mu)./sigma0;
|
|
% z = bsxfun(@minus,x, mu);
|
|
% z = bsxfun(@rdivide, z, sigma0);
|
|
|