# Modifications to hist.m

5 messages
Open this post in threaded view
|

## Modifications to hist.m

 I propose the following patch to hist.m; it results in about 2.5x speedup. octave:1> im=rand(500^2,1); octave:2> jnk= hist(im,100); tic;v1=hist(im,100);toc ans = 7.1460 octave:3> jnk= hist_orig(im,100);tic;v2=hist_orig(im,100);toc ans = 17.604 octave:4> std(v1-v2) ans = 0 \$ diff -u hist.m /usr/local/oct2145/share/octave/2.1.45/m/plot/hist.m --- hist.m      2003-03-15 15:47:16.000000000 -0500 +++ /usr/local/oct2145/share/octave/2.1.45/m/plot/hist.m        2003-02-27 19:22 :32.000000000 -0500 @@ -87,11 +87,12 @@      endif    endif -  cumfreq = [zeros (1, n), length(y) ]; -  for i = 1:n-1 -    cumfreq (i+1) = sum (y < cutoff (i)); -  end -  freq= diff(cumfreq); +  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 (nargin == 3)      ## Normalise the histogram. Andy
Open this post in threaded view
|

## Re: Modifications to hist.m

Open this post in threaded view
|

## Re: Modifications to hist.m

 Paul, That's some really nice code - Tried to think of a faster solution that would not eat too much more memory, but yours is great. It's slightly more expensive for a small number of bins, but the crossover is about 30, in my example r=[]; i=0; for sz= [100, 200, 500];     x=rand(sz^2,1);     for n=[10,30,100];  i++;       tic; h1=hist1(x,n);r(i,1)= toc;       tic; h2=hist2(x,n);r(i,2)= toc;       tic; h3=hist3(x,n);r(i,3)= toc;     end end           CURRENT   ANDY    PAUL SZ=100    n=10    0.059   0.032   0.077    n=30    0.172   0.079   0.085    n=100   0.575   0.245   0.086 SZ=200    n=10    0.242   0.109   0.344    n=30    0.754   0.312   0.335    n=100   2.526   1.016   0.350 SZ=500    n=10    1.688   0.733   2.831    n=30    9.110   2.237   4.967    n=100  32.847  13.291   5.431 Andy