1 function [i,v,m] = zscore(i,DIM)
2 % ZSCORE removes the mean and normalizes the data
3 % to a variance of 1. Can be used for Pre-Whitening of the data, too.
5 % [z,r,m] = zscore(x,DIM)
6 % z z-score of x along dimension DIM
7 % r is the inverse of the standard deviation
10 % The data x can be reconstrated with
11 % x = z*diag(1./r) + repmat(m,size(z)./size(m))
12 % z = x*diag(r) - repmat(m.*v,size(z)./size(m))
17 % default or []: first DIMENSION, with more than 1 element
19 % see also: SUMSKIPNAN, MEAN, STD, DETREND
22 % [1] http://mathworld.wolfram.com/z-Score.html
24 % This program is free software; you can redistribute it and/or modify
25 % it under the terms of the GNU General Public License as published by
26 % the Free Software Foundation; either version 2 of the License, or
27 % (at your option) any later version.
29 % This program is distributed in the hope that it will be useful,
30 % but WITHOUT ANY WARRANTY; without even the implied warranty of
31 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 % GNU General Public License for more details.
34 % You should have received a copy of the GNU General Public License
35 % along with this program; If not, see <http://www.gnu.org/licenses/>.
38 % $Id: zscore.m 8223 2011-04-20 09:16:06Z schloegl $
39 % Copyright (C) 2000-2003,2009 by Alois Schloegl <alois.schloegl@gmail.com>
40 % This function is part of the NaN-toolbox
41 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
44 if any(size(i)==0); return; end;
53 DIM=min(find(size(i)>1));
54 if isempty(DIM), DIM=1; end;
60 i = i-repmat(m,size(i)./size(m)); % remove mean
61 v = 1./sqrt(mean(i.^2,DIM,W));
62 i = i.*repmat(v,size(i)./size(v)); % scale to var=1