Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function varargout=histn(varargin)
%HISTN N-dimensional histogram.
% P=HISTN(X) bins the N-dimensional density of X(:,1),...
% X(:,N) into a 10x10 equally spaced matrix and returns it
% in P.
%
% P=HISTN(X1,X2,...,Xn) bins the M-dimensional density of
% X1(i),...,XN(i) into a 10x10 equally spaced matrix and
% returns it in P, where M is the sum of the numbers of
% columns of the input arguments.
%
% P=HISTN(X1,X2,...,Xn,L), where L is a scalar, uses a lag L
% between the input vectors. When only one input vector is
% given, the lag has an effect only if this vector has only
% one column. The latter corresponds to P=HISTN(X,X,L).
%
% P=HISTN(X1,X2,...,Xn,K,L), where K and L are scalars, uses
% K bins and a lag L.
%
% [P,J]=HISTN(...) returns the matrix P and the N-dimensional
% vector J containing the N-dimensional density matrix and the
% bin location for X1,...,Xn.
%
% HISTN(...) without any output arguments produces a histogram plot.
%
% Examples: x = randn(10000,3);
% histn(x)
%
% See also HIST, HIST2, BAR3, MI.
% Copyright (c) 2002-2003
% Andre Sitz, Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% $Date$
% $Revision$
%
% $Log$
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or any later version.
%try
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check the input
error(nargchk(1,16,nargin));
if nargout>2, error('Too many output arguments'), end
nogui=1;
lag=[];
nbin=10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% read the input
x={}; sm_length=[]; sum_m=0;
if isnumeric(varargin{1})
for i=1:nargin
if isnumeric(varargin{i}) & max(size(varargin{i}))>1
y=varargin{i};
if size(y,1)==1, y=y'; end
my=size(y,2); sum_m=sum_m+my;
mx=size(y,1);
if isempty(sm_length), sm_length=mx; end
if mx < sm_length, sm_length=mx; end
x(i)={y};
end
end
m=size(x,2); if sum_m==1; lag=1; x(2)=x(1); sum_m=2; m=2; end
if isempty(x), error('Not a valid input vector.'), end
i_double=find(cellfun('isclass',varargin,'double'));
i_char=find(cellfun('isclass',varargin,'char'));
if ~isempty(i_char)
if findstr(lower(varargin{i_char(1)}),'n')
nogui=1;
end
if findstr(lower(varargin{i_char(1)}),'s')
nogui=2;
end
if findstr(lower(varargin{i_char(1)}),'g')
nogui=0;nogui=1;
end
end
if length(i_double)>1
if max(size(varargin{i_double(end-1)}))==1
nbin=varargin{i_double(end-1)}; else nbin=10;
end
end
if max(size(varargin{i_double(end)}))==1
lag=varargin{i_double(end)}; else if isempty(lag), lag=0; end
end
else
error('Input must be numeric.')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% splash the GPL
if nogui~=2;
splash_gpl('histn');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% estimate histogram
%if size(x,1)<size(x,2), x=x';end
for k=1:m;
x1(k)={(x{k}(1:sm_length,:)-repmat(min(x{k}(1:sm_length,:)),length(x{k}(1:sm_length,:)),1))./repmat(max(x{k}(1:sm_length,:)-repmat(min(x{k}(1:sm_length,:)),length(x{k}(1:sm_length,:)),1)),length(x{k}(1:sm_length,:)),1)-eps};
if m>1 & sm_length/(m-1)<=lag, lag=floor(sm_length/(m-1))-1; warning(['Lag is too large. Using ',num2str(lag),' instead.']), end
end
temp=0; kl=0;
for k=1:m
for l=1:size(x1{k},2)
temp=temp+fix(x1{k}(1+(lag*(k-1)):(end-(lag*(m-k))),l)*nbin)*nbin^(kl);
kl=kl+1;
end
end
p=histc(temp,0:(nbin^sum_m-1))';
p2=eval(['reshape(p',repmat([',nbin'],1,sum_m),')/length(x);']);
kl=0;
for k=1:m
for l=1:size(x1{k},2)
kl=kl+1;
minx = min(min(x{k}(:,l))); maxx = max(max(x{k}(:,l)));
if minx == maxx, minx = minx - floor(nbin/2) - 0.5; maxx = maxx + ceil(nbin/2) - 0.5; end
bins(kl,:)=minx:(maxx-minx)/(nbin-1):maxx+eps;
end
end
if nargout==0
p3=eval(['p2(:,:',repmat([',round(nbin/2)'],1,sum_m-2),')']);
bar3(bins(2,:),p3)
dhy=bins(2,2)-bins(2,1);
set(gca,'xlim',[0 nbin+1-.1],'ylim',[bins(2,1)-dhy bins(2,end)+dhy])
hx=get(gca,'xtick'); dhx=diff(hx);
set(gca,'xtick',[1:dhx(1):nbin+1])
if dhx>1
tx=num2str(fix(100*[bins(1,1):(bins(1,end)-bins(1,1))/(length(hx)-1):bins(1,end)]')/100);
else
tx=num2str(fix(100*[bins(1,:)]')/100);
end
set(gca,'xticklabel',tx)
xlabel('x'), ylabel('y')
elseif nargout==1
varargout{1}=p2;
elseif nargout==2
varargout{1}=p2;
varargout{2}=bins';
end