histo.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. % [N,X] = histo(MTX, nbinsOrBinsize, binCenter);
  2. %
  3. % Compute a histogram of (all) elements of MTX. N contains the histogram
  4. % counts, X is a vector containg the centers of the histogram bins.
  5. %
  6. % nbinsOrBinsize (optional, default = 101) specifies either
  7. % the number of histogram bins, or the negative of the binsize.
  8. %
  9. % binCenter (optional, default = mean2(MTX)) specifies a center position
  10. % for (any one of) the histogram bins.
  11. %
  12. % How does this differ from MatLab's HIST function? This function:
  13. % - allows uniformly spaced bins only.
  14. % +/- operates on all elements of MTX, instead of columnwise.
  15. % + is much faster (approximately a factor of 80 on my machine).
  16. % + allows specification of number of bins OR binsize. Default=101 bins.
  17. % + allows (optional) specification of binCenter.
  18. % Eero Simoncelli, 3/97.
  19. function [N, X] = histo(mtx, nbins, binCtr)
  20. %% NOTE: THIS CODE IS NOT ACTUALLY USED! (MEX FILE IS CALLED INSTEAD)
  21. fprintf(1,'WARNING: You should compile the MEX version of "histo.c",\n found in the MEX subdirectory of matlabPyrTools, and put it in your matlab path. It is MUCH faster.\n');
  22. mtx = mtx(:);
  23. %------------------------------------------------------------
  24. %% OPTIONAL ARGS:
  25. [mn,mx] = range2(mtx);
  26. if (exist('binCtr') ~= 1)
  27. binCtr = mean(mtx);
  28. end
  29. if (exist('nbins') == 1)
  30. if (nbins < 0)
  31. binSize = -nbins;
  32. else
  33. binSize = ((mx-mn)/nbins);
  34. tmpNbins = round((mx-binCtr)/binSize) - round((mn-binCtr)/binSize);
  35. if (tmpNbins ~= nbins)
  36. warning('Using %d bins instead of requested number (%d)',tmpNbins,nbins);
  37. end
  38. end
  39. else
  40. binSize = ((mx-mn)/101);
  41. end
  42. firstBin = binCtr + binSize*round( (mn-binCtr)/binSize );
  43. tmpNbins = round((mx-binCtr)/binSize) - round((mn-binCtr)/binSize);
  44. bins = firstBin + binSize*[0:tmpNbins];
  45. [N, X] = hist(mtx, bins);