comparison m-toolbox/classes/@ao/corr.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % CORR estimate linear correlation coefficients.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: CORR estimate linear correlation coefficients.
5 %
6 % The method returns a P-by-P matrix containing the pairwise
7 % linear correlation coefficient between each pair of columns
8 % in the N-by-P matrix X formed from the length-N vectors of
9 % the P input AOs. The coefficients are calculated using
10 % Pearson's product-moment method.
11 %
12 % CALL: >> c = corr(a,b)
13 % >> c = corr(a,b,c,...)
14 %
15 % INPUTS: a,b,c,... - input analysis objects
16 %
17 % OUTPUTS: c - output analysis object containing the correlation matrix.
18 %
19 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'corr')">Parameters Description</a>
20 %
21 % VERSION: $Id: corr.m,v 1.9 2011/04/08 08:56:18 hewitson Exp $
22 %
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24
25 function varargout = corr(varargin)
26
27 % Check if this is a call for parameters
28 if utils.helper.isinfocall(varargin{:})
29 varargout{1} = getInfo(varargin{3});
30 return
31 end
32
33 import utils.const.*
34 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
35
36 % Collect input variable names
37 in_names = cell(size(varargin));
38 for ii = 1:nargin,in_names{ii} = inputname(ii);end
39
40 % Collect all AOs and plists
41 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
42
43 if nargout == 0
44 error('### corr cannot be used as a modifier. Please give an output variable.');
45 end
46
47 if numel(as) < 2
48 error('### corr requires at least two input AOs to work.');
49 end
50
51 % Convolute the data
52 smat = [];
53 inunits = unit;
54 name = '';
55 desc = '';
56 for jj=1:numel(as)
57 smat = [smat as(jj).data.getY];
58 inunits = inunits .* as(jj).data.yunits;
59 name = strcat(name, [',' ao_invars{jj}]);
60 desc = strcat(desc, [' ' as(jj).description]);
61 end
62 desc = strtrim(desc);
63
64 % compute the sample correlation using Pearson's product-moment coefficient
65 Cv = cov(smat);
66 C = zeros(size(Cv));
67 for ii=1:size(Cv, 1)
68 for kk=1:size(Cv,2)
69 C(ii,kk) = Cv(ii,kk) ./ (sqrt(Cv(ii,ii))*sqrt(Cv(kk,kk)));
70 end
71 end
72
73 bs = ao(cdata(C));
74 bs.name = sprintf('corr(%s)', name(2:end));
75 bs.description = desc;
76 bs.data.setYunits(inunits);
77 bs.addHistory(getInfo('None'), getDefaultPlist, ao_invars, [as(:).hist]);
78
79 % Set output
80 if nargout == numel(bs)
81 % List of outputs
82 for ii = 1:numel(bs)
83 varargout{ii} = bs(ii);
84 end
85 else
86 % Single output
87 varargout{1} = bs;
88 end
89 end
90
91 %--------------------------------------------------------------------------
92 % Get Info Object
93 %--------------------------------------------------------------------------
94 function ii = getInfo(varargin)
95 if nargin == 1 && strcmpi(varargin{1}, 'None')
96 sets = {};
97 pls = [];
98 else
99 sets = {'Default'};
100 pls = getDefaultPlist;
101 end
102 % Build info object
103 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: corr.m,v 1.9 2011/04/08 08:56:18 hewitson Exp $', sets, pls);
104 ii.setModifier(false);
105 ii.setArgsmin(2);
106 end
107
108 %--------------------------------------------------------------------------
109 % Get Default Plist
110 %--------------------------------------------------------------------------
111
112 function plout = getDefaultPlist()
113 persistent pl;
114 if exist('pl', 'var')==0 || isempty(pl)
115 pl = buildplist();
116 end
117 plout = pl;
118 end
119
120 function pl_default = buildplist()
121 pl_default = plist.EMPTY_PLIST;
122 end
123