1 function R = histo2(Y, W)
2 % HISTO2 calculates histogram for multiple columns with separate bin values
3 % for each data column.
8 % W weight vector containing weights of each sample,
9 % number of rows of Y and W must match.
10 % default W=[] indicates that each sample is weighted with 1.
13 % R is a struct with th fields
14 % R.X the bin-values, bin-values are computed separately for each
15 % data column, thus R.X is a matrix, each column contains the
16 % the bin values of for each data column, unused elements are indicated with NaN.
17 % In order to have common bin values, use HISTO3.
18 % R.H is the frequency of occurence of value X
19 % R.N are the number of valid (not NaN) samples (i.e. sum of weights)
21 % more histogram-based results can be obtained by HIST2RES2
23 % see also: HISTO, HISTO2, HISTO3, HISTO4
26 % C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963).
28 % $Id: histo2.m 8383 2011-07-16 20:06:59Z schloegl $
29 % Copyright (C) 1996-2002,2008,2011 by Alois Schloegl <alois.schloegl@gmail.com>
30 % This is part of the TSA-toolbox
31 % http://hci.tugraz.at/~schloegl/matlab/tsa/
33 % This program is free software: you can redistribute it and/or modify
34 % it under the terms of the GNU General Public License as published by
35 % the Free Software Foundation, either version 3 of the License, or
36 % (at your option) any later version.
38 % This program is distributed in the hope that it will be useful,
39 % but WITHOUT ANY WARRANTY; without even the implied warranty of
40 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 % GNU General Public License for more details.
43 % You should have received a copy of the GNU General Public License
44 % along with this program. If not, see <http://www.gnu.org/licenses/>.
47 %%%%% check input arguments %%%%%
52 if ~isempty(W) && (yr ~= numel(W)),
53 error('number of rows of Y does not match number of elements in W');
56 %%%%% identify all possible X's and generate overall Histogram %%%%%
57 N = sum(~isnan(Y), 1);
62 [sY, idx] = sort(Y,1);
63 W = cumsum(W(idx)); %% W becomes cumulative sum
65 [ix,iy] = find( diff(sY, [], 1) > 0);
69 tmp = [ix(iy==k); N(k)];
73 H(1:nn1,k) = [tmp(1); diff(tmp)];
75 %%% Note that W is the cumulative sum
76 H(1:nn1,k) = [W(tmp(1),k); diff(W(tmp,k))];
79 X(1:nn1, k) = sY(tmp, k);
84 H (1+nn1:nn0, k) = NaN;
85 X (1+nn1:nn0, k) = NaN;
87 H (1+nn0:nn1, 1:k-1) = NaN;
88 X (1+nn0:nn1, 1:k-1) = NaN;
93 R.datatype = 'HISTOGRAM';