comparison scripts/plot/hist.m @ 4407:16e8acbd19d5

[project @ 2003-05-05 19:00:56 by jwe]
author jwe
date Mon, 05 May 2003 19:00:56 +0000
parents 22bd65326ec1
children 1541c3ed2c93
comparison
equal deleted inserted replaced
4406:28f1efef88f7 4407:16e8acbd19d5
75 tmp = sort (x); 75 tmp = sort (x);
76 if (any (tmp != x)) 76 if (any (tmp != x))
77 warning ("hist: bin values not sorted on input"); 77 warning ("hist: bin values not sorted on input");
78 x = tmp; 78 x = tmp;
79 endif 79 endif
80 n = length (x); 80 cutoff = (x(1:end-1) + x(2:end)) / 2;
81 cutoff = zeros (1, n-1);
82 for i = 1:n-1
83 cutoff (i) = (x (i) + x (i+1)) / 2;
84 endfor
85 else 81 else
86 error ("hist: second argument must be a scalar or a vector"); 82 error ("hist: second argument must be a scalar or a vector");
87 endif 83 endif
88 endif 84 endif
89 85
90 freq = zeros (1, n); 86 if (n < 30)
91 freq (1) = sum (y < cutoff (1)); 87 ## The following algorithm works fastest for n less than about 30.
92 for i = 2:n-1 88 chist = [zeros(n,1); length(y)];
93 freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i)); 89 for i = 1:n-1
94 endfor 90 chist(i+1) = sum (y < cutoff(i));
95 freq (n) = sum (y >= cutoff (n-1)); 91 endfor
92 else
93 ## The following algorithm works fastest for n greater than about 30.
94 ## Put cutoff elements between boundaries, integrate over all
95 ## elements, keep totals at boundaries.
96 [s, idx] = sort ([cutoff(:); y(:)]);
97 chist = cumsum(idx>n);
98 chist = [0; chist(idx<n); chist(end)];
99 endif
100
101 freq= diff(chist)';
96 102
97 if (nargin == 3) 103 if (nargin == 3)
98 ## Normalise the histogram. 104 ## Normalise the histogram.
99 freq = freq / length(y) * norm; 105 freq = freq / length (y) * norm;
100 endif 106 endif
101 107
102 if (nargout > 0) 108 if (nargout > 0)
103 nn = freq; 109 nn = freq;
104 xx = x; 110 xx = x;