NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/plots/density.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function [rrho, xxvec, yyvec] = density(x, y, dx, dy, xy0);
2 % [rho, xvec, yvec] = density(x, y, dx, dy, [x0 x1 y0 y1]); 
3 %
4 %  2-D probability density plot.
5 %
6 %  Input:  Vectors x and y of equal length. If dx and/or dy are omitted, the
7 %          default is 75 bins in each direction.
8 %     
9 %          If dx or dy is negative, then the variable is taken as the number of
10 %          bins rather than a grid resolution.
11 %
12 %          The vector containing the limits can be padded with NaNs if only
13 %          certain limits are desired, e g if x0 and y1 are wanted:
14 %
15 %            density(x, y, [.5 nan nan 45])
16 %
17 %  Output: The density matrix RHO together with vectors XVEC and YVEC.
18 %          If no output arguments are specified, DENSITY will plot the density
19 %          function with the prescribed axes using PCOLOR.
20 %
21 %  Requires bin.m. Tested under MatLab 4.2, 5.0, and 5.1.
22 %
23 %  See also bin.m for further details about dx, x0, etc, and ffgrid.m.
24 %
25
26 % 1.9.97 Oyvind.Breivik@gfi.uib.no.
27 %
28 % Oyvind Breivik
29 % Department of Geophysics
30 % University of Bergen
31 % NORWAY
32
33 DX = -75;  % Default grid size
34
35 x = x(:);
36
37 if nargin < 2
38  y = x;
39 end
40
41 y = y(:);
42
43 xy = NaN*ones(1,4);
44
45 if (nargin < 4)
46  xy0 = min(x);
47 end
48
49 if (nargin == 3 & length(dx) > 1)
50  xy0 = dx;
51  dx = DX;
52 end
53
54 if nargin < 3
55  dx = DX;
56 end
57
58 if (nargin == 4 & length(dy) > 1)
59  xy0 = dy;
60  dy = dx;
61 end
62 if nargin < 4
63  dy = dx;
64 end
65
66 nxy = length(xy0);
67 xy(1:nxy) = xy0;
68
69 if (isnan(xy(4)))
70  xy(4) = nmax(y);
71 end
72 if (isnan(xy(3)))
73  xy(3) = nmin(y);
74 end
75 if (isnan(xy(2)))
76  xy(2) = nmax(x);
77 end
78 if (isnan(xy(1)))
79  xy(1) = nmin(x);
80 end
81 x0 = xy(1); x1 = xy(2); y0 = xy(3); y1 = xy(4);
82
83 if (dx < 0)
84  dx = (x1 - x0)/abs(dx);
85 end
86 if (dy < 0)
87  dy = (y1 - y0)/abs(dy);
88 end
89
90 ix = bin(x, dx, x0, x1);
91 iy = bin(y, dy, y0, y1); % bin data in (x,y)-space
92
93 xvec = x0:dx:x1;
94 yvec = y0:dy:y1;
95
96 nx = length(xvec);
97 ny = length(yvec);
98
99 inx = (ix >= 1) & (ix <= nx);
100 iny = (iy >= 1) & (iy <= ny);
101 in = (inx & iny);
102 ix = ix(in); iy = iy(in);
103 N = length(ix); % how many datapoints are left now?
104
105 rho = zeros(length(xvec), length(yvec)) + eps;
106
107 for i = 1:N
108  rho(ix(i), iy(i)) = rho(ix(i), iy(i)) + 1;
109 end
110
111 rho = rho/(N*dx*dy); % Density is n per dx per dy
112
113 rho = rho'; % Get in shape
114
115 if nargout == 0
116  pcolor(xvec, yvec, rho)
117  colorbar
118  colormap jet
119  xlabel(inputname(1))
120  ylabel(inputname(2))
121  dum = size(rho');
122  str = sprintf('%d data points, grid: %dx%d', N, dum(1)-1, dum(2)-1);
123  title(str);
124 end
125
126 if nargout > 0
127  rrho = rho;
128 end
129
130 if nargout > 1
131  xxvec = xvec;
132  yyvec = yvec;
133 end
Note: See TracBrowser for help on using the browser.