[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Octave 2.1.47 available for ftp
From: |
Andy Adler |
Subject: |
Re: Octave 2.1.47 available for ftp |
Date: |
Sat, 3 May 2003 11:35:32 -0400 (EDT) |
I'm still campaining to have a new version of hist.m
incorporated into octave.
The attached patch combines the best of my and Paul Kienzle's
code (The efficiency break is at n=30 bins).
Here is a table of timing comparisons:
n ORIGINAL AAversion PKversion ThisVersion
3 0.287 0.1920 1.7170 0.1910
10 1.125 0.4880 1.7290 0.4900
20 2.278 0.9460 1.7140 0.9480
30 3.823 1.5690 1.7650 1.4000
40 5.116 2.0880 1.7770 1.4020
50 6.572 2.6100 1.7600 1.3930
60 7.743 3.1220 1.7800 1.4030
100 12.881 5.1770 1.7710 1.4010
Test Code:
jnk=rand(400);
for n=[3,10,20,30,40,50,60,100];
disp(n);
tic; x1=hist1(jnk(:),n); toc,
tic; x2=hist2(jnk(:),n); toc,
tic; x3=hist3(jnk(:),n); toc,
tic; x4=hist4(jnk(:),n); toc,
if ~( all(x1==x2) && all(x2==x3) && all(x3==x4));error('bad');end;
end
PATCH:
--- hist.m.orig 2003-05-03 11:24:48.000000000 -0400
+++ hist.m 2003-05-03 11:24:38.000000000 -0400
@@ -42,6 +42,7 @@
## @seealso{bar}
## Author: jwe
+## Modifications May 2003 by A.Adler, using code from P.Kienzle
function [nn, xx] = hist (y, x, norm)
@@ -78,21 +79,28 @@
x = tmp;
endif
n = length (x);
- cutoff = zeros (1, n-1);
- for i = 1:n-1
- cutoff (i) = (x (i) + x (i+1)) / 2;
- endfor
+ cutoff = ( x(1:n-1) + x(2:n) ) / 2;
else
error ("hist: second argument must be a scalar or a vector");
endif
endif
- freq = zeros (1, n);
- freq (1) = sum (y < cutoff (1));
- for i = 2:n-1
- freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i));
- endfor
- freq (n) = sum (y >= cutoff (n-1));
+if n < 30
+ % algorithm works fastest for n<30
+ chist = [zeros(n,1); length(y) ];
+ for i = 1:n-1
+ chist (i+1) = sum (y < cutoff (i));
+ end
+else
+ % algorithm works fastest for n>30
+ % put cutoff elements between boundaries
+ % integrate over all elements, keep totals at boundaries
+
+ [s, idx] = sort ( [cutoff(:); y(:)] );
+ chist = cumsum(idx>n);
+ chist = [0; chist(idx<n); chist(length(chist))];
+end
+ freq= diff(chist)';
if (nargin == 3)
## Normalise the histogram.