]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/zscore.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / zscore.m
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. 
4 %
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
8 %   m   is the mean of x
9 %
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))  
13 %
14 % DIM   dimension
15 %       1: STATS of columns
16 %       2: STATS of rows
17 %       default or []: first DIMENSION, with more than 1 element
18 %
19 % see also: SUMSKIPNAN, MEAN, STD, DETREND
20 %
21 % REFERENCE(S):
22 % [1] http://mathworld.wolfram.com/z-Score.html
23
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.
28 %
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.
33 %
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/>.
36
37
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/
42
43
44 if any(size(i)==0); return; end;
45
46 if nargin<2
47         DIM=[]; 
48 end
49 if nargin<3
50         W = []; 
51 end
52 if isempty(DIM), 
53         DIM=min(find(size(i)>1));
54         if isempty(DIM), DIM=1; end;
55 end;
56
57
58 % pre-whitening
59 m = mean(i,DIM,W);
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
63
64