-
Notifications
You must be signed in to change notification settings - Fork 3
/
bindens.m
64 lines (56 loc) · 1.73 KB
/
bindens.m
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
function [h,c11,cmn,hh,ybine]=bindens(x,y,nxbins,nybins)
% [h,c11,cmn,hh,ybine]=BINDENS(x,y,nxbins,nybins)
%
% Constructs a two-dimensional histogram
%
% INPUT:
%
% x,y The data vectors
% nxbins, nybins The number of bins in the x and y direction
%
% OUTPUT:
%
% h The '2D' histogram, an auxiliary quantity
% c11,cmn The centers of the top left and bottom right of this histogram
% hh Globally normalized histogram, THAT is what you want to plot
% ybine The y bin edges that are being used
%
% EXAMPLE:
%
% N=1000; x=randn(randi(N),1); y=randn(length(x),1);
% [h,c11,cmn,hh]=bindens(x,y);
% subplot(121); plot(x,y,'.'); axis image; axis([-3.5 3.5 -3.5 3.5])
% subplot(122); imagefnan(c11,cmn,hh,flipud(gray)); axis image; axis([-3.5 3.5 -3.5 3.5])
% hold on; pp=plot(x,y,'w.'); hold off
%
% SEE ALSO:
%
% ROW2STATS, BIN2STATS, (HIST2D)
%
% Last modified by fjsimons-at-alum.mit.edu, 08/20/2020
% Specify defaults
defval('nxbins',10)
defval('nybins',10)
defval('xbin',range(x)/nxbins);
defval('ybin',range(y)/nybins);
% Specify the y-bin edges, that's one more than there are bins
ybine=linspace(min(y),max(y),nybins+1);
% Sort the data in the first column
[x,I]=sort(x,1);
% And have the second column follow
y=y(I);
% Bin the x-data
ix=ceil((x-min(x))/xbin);
ix=ix+(ix==0);
% Also must put in nans for the bins that didn't happen
adix=skip(1:max(ix),unique(ix));
ix=[ix ; adix(:)];
y=[y ; nan(size(adix(:)))];
% Now use ROW2STATS to get the y-histograms
[g,s,h,hh]=row2stats(ix,y,ybine);
% Do not do flipud as the histogram treats the bins as one-sided to the
% right but rather find the pixel-centered coordinates of the histogram?
c11(1)=min(x)+xbin/2;
cmn(1)=max(x)-xbin/2;
c11(2)=min(y)+ybin/2;
cmn(2)=max(y)-ybin/2;