annotate scripts/plot/draw/hist.m @ 28523:68000a4df5a8

hist.m: use different method for preserving accuracy of arithmetic on ranges * hist.m: When doing arithmetic on ranges, start with integer range values and perform division last.
author John W. Eaton <jwe@octave.org>
date Wed, 01 Jul 2020 15:27:22 -0400
parents bd51beb6205e
children 376ecc6794e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 1994-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24432
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24432
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
13 ## (at your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6586
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24432
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
25
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3175
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
27 ## @deftypefn {} {} hist (@var{y})
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
28 ## @deftypefnx {} {} hist (@var{y}, @var{nbins})
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
29 ## @deftypefnx {} {} hist (@var{y}, @var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
30 ## @deftypefnx {} {} hist (@var{y}, @var{x}, @var{norm})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
31 ## @deftypefnx {} {} hist (@dots{}, @var{prop}, @var{val}, @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
32 ## @deftypefnx {} {} hist (@var{hax}, @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20231
diff changeset
33 ## @deftypefnx {} {[@var{nn}, @var{xx}] =} hist (@dots{})
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
34 ## Produce histogram counts or plots.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3407
diff changeset
35 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11575
diff changeset
36 ## With one vector input argument, @var{y}, plot a histogram of the values
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11575
diff changeset
37 ## with 10 bins. The range of the histogram bins is determined by the
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
38 ## range of the data (difference between maximum and minimum value in @var{y}).
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
39 ## Extreme values are lumped into the first and last bins. If @var{y} is a
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
40 ## matrix then plot a histogram where each bin contains one bar per input
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
41 ## column of @var{y}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3407
diff changeset
42 ##
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
43 ## If the optional second argument is a scalar, @var{nbins}, it defines the
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
44 ## number of bins.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3407
diff changeset
45 ##
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
46 ## If the optional second argument is a vector, @var{x}, it defines the centers
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
47 ## of the bins. The width of the bins is determined from the adjacent values
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
48 ## in the vector. The total number of bins is @code{numel (@var{x})}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3407
diff changeset
49 ##
11575
d6619410e79c Spellcheck documentation before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents: 11563
diff changeset
50 ## If a third argument is provided, the histogram is normalized such that
3597
26662775f4e9 [project @ 2000-02-12 02:24:41 by jwe]
jwe
parents: 3426
diff changeset
51 ## the sum of the bars is equal to @var{norm}.
26662775f4e9 [project @ 2000-02-12 02:24:41 by jwe]
jwe
parents: 3426
diff changeset
52 ##
11351
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
53 ## The histogram's appearance may be modified by specifying property/value
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
54 ## pairs. For example, the face and edge color may be modified:
11351
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
55 ##
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
56 ## @example
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
57 ## @group
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
58 ## hist (randn (1, 100), 25, "facecolor", "r", "edgecolor", "b");
11351
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
59 ## @end group
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
60 ## @end example
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
61 ##
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
62 ## @noindent
17158
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
63 ## The histogram's colors also depend upon the current colormap.
11351
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
64 ##
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
65 ## @example
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
66 ## @group
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
67 ## hist (rand (10, 3));
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
68 ## colormap (summer ());
11351
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
69 ## @end group
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
70 ## @end example
bff585d759cf improved the help text in hist.m
Doug Stewart <doug.dastew@gmail.com>
parents: 10549
diff changeset
71 ##
24431
0c6cedafc71e doc: Use 'axes' rather than 'axis' appropriately in docstrings.
Rik <rik@octave.org>
parents: 23219
diff changeset
72 ## If the first argument @var{hax} is an axes handle, then plot into this axes,
17158
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
73 ## rather than the current axes returned by @code{gca}.
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
74 ##
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
75 ## If an output is requested then no plot is made. Instead, return the values
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
76 ## @var{nn} (numbers of elements) and @var{xx} (bin centers) such that
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
77 ## @code{bar (@var{xx}, @var{nn})} will plot the histogram.
17158
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
78 ##
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14872
diff changeset
79 ## @seealso{histc, bar, pie, rose}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3175
diff changeset
80 ## @end deftypefn
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
81
17158
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
82 function [nn, xx] = hist (varargin)
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
83
b491ef539071 hist.m: Accept an axis handle as first input (bug #38508).
Rik <rik@octave.org>
parents: 17122
diff changeset
84 [hax, varargin, nargin] = __plt_get_axis_arg__ ("hist", varargin{:});
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
85
7112
f90a8188c9c2 [project @ 2007-11-07 00:24:11 by dbateman]
dbateman
parents: 7017
diff changeset
86 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5746
diff changeset
87 print_usage ();
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
88 endif
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18777
diff changeset
89
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
90 ## Process Y argument
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
91 iarg = 1;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
92 y = varargin{iarg++};
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
93
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
94 if (! isreal (y))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
95 error ("hist: Y must be real-valued");
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
96 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2314
diff changeset
97
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
98 arg_is_vector = isvector (y);
17487
be7e8b91c6b1 hist.m: Overhaul function.
Rik <rik@octave.org>
parents: 17162
diff changeset
99 if (arg_is_vector)
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
100 y = y(:);
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
101 endif
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
102
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
103 yfinite = y(isfinite (y))(:);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
104 max_val = max (yfinite);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
105 min_val = min (yfinite);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
106 ## Do not convert if input is of class single (or if already is double).
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
107 if (! isfloat (y))
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
108 max_val = double (max_val);
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
109 min_val = double (min_val);
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
110 endif
17487
be7e8b91c6b1 hist.m: Overhaul function.
Rik <rik@octave.org>
parents: 17162
diff changeset
111
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
112 ## Equidistant entries allow much more efficient algorithms.
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
113 equal_bin_spacing = true;
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
114
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
115 ## Process possible second argument
7112
f90a8188c9c2 [project @ 2007-11-07 00:24:11 by dbateman]
dbateman
parents: 7017
diff changeset
116 if (nargin == 1 || ischar (varargin{iarg}))
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
117 n = 10;
28523
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
118 ## Use integer range values and perform division last to preserve
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
119 ## accuracy.
25630
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
120 if (min_val != max_val)
28523
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
121 x = 1:2:2*n;
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
122 x = ((max_val - min_val) * x + 2*n*min_val) / (2*n);
25630
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
123 else
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
124 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val;
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
125 endif
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
126 x = x.'; # Convert to matrix;
3597
26662775f4e9 [project @ 2000-02-12 02:24:41 by jwe]
jwe
parents: 3426
diff changeset
127 else
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
128 ## Parse bin specification argument
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7191
diff changeset
129 x = varargin{iarg++};
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
130 if (! isreal (x))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
131 error ("hist: bin specification must be a numeric scalar or vector");
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
132 endif
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
133
27164
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
134 ## Convert integer types or a single specification of N bins to double
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
135 if (! isfloat (x) || isscalar (x))
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
136 x = double (x);
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
137 endif
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
138
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3690
diff changeset
139 if (isscalar (x))
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
140 n = fix (x);
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
141 if (n <= 0)
17487
be7e8b91c6b1 hist.m: Overhaul function.
Rik <rik@octave.org>
parents: 17162
diff changeset
142 error ("hist: number of bins NBINS must be positive");
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
143 endif
28523
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
144 ## Use integer range values and perform division last to preserve
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
145 ## accuracy.
25630
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
146 if (min_val != max_val)
28523
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
147 x = 1:2:2*n;
68000a4df5a8 hist.m: use different method for preserving accuracy of arithmetic on ranges
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
148 x = ((max_val - min_val) * x + 2*n*min_val) / (2*n);
25630
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
149 else
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
150 x = (-floor ((n-1)/2):ceil ((n-1)/2)) + min_val;
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
151 endif
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
152 x = x.'; # Convert to matrix;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
153 elseif (isvector (x))
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
154 equal_bin_spacing = strcmp (typeinfo (x), "range");
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
155 if (! equal_bin_spacing)
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
156 diffs = diff (x);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
157 if (all (diffs == diffs(1)))
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
158 equal_bin_spacing = true;
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
159 endif
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
160 endif
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
161 x = x(:);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
162 if (! issorted (x))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
163 warning ("hist: bin values X not sorted on input");
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
164 x = sort (x);
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
165 endif
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
166 else
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
167 error ("hist: bin specification must be a scalar or vector");
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
168 endif
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
169 endif
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
170
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
171 ## Check for third argument (normalization)
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
172 norm = false;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
173 if (nargin > 2 && ! ischar (varargin{iarg}))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
174 norm = varargin{iarg++};
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
175 if (! isnumeric (norm) || ! all (norm > 0))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
176 error ("hist: NORM must be a numeric constant > 0");
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
177 endif
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
178 endif
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
179
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
180 ## Perform histogram calculation
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
181 cutoff = (x(1:end-1,:) + x(2:end,:)) / 2;
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents: 7112
diff changeset
182
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
183 n = rows (x);
7566
b3acdf1c41a5 hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents: 7208
diff changeset
184 y_nc = columns (y);
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
185
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
186 if (n < 11 * (1 + (! equal_bin_spacing)))
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
187 ## The following algorithm works fastest for small n.
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
188 nanidx = isnan (y);
7566
b3acdf1c41a5 hist: avoid temps; allow matrix args when number of bins > 30
John W. Eaton <jwe@octave.org>
parents: 7208
diff changeset
189 chist = zeros (n+1, y_nc);
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4030
diff changeset
190 for i = 1:n-1
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
191 chist(i+1,:) = sum (y <= cutoff(i));
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4030
diff changeset
192 endfor
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
193 chist(n+1,:) = sum (! nanidx);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
194
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
195 freq = diff (chist);
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4030
diff changeset
196 else
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
197 ## Lookup is more efficient if y is sorted, but sorting costs.
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
198 if (! equal_bin_spacing && n > sqrt (rows (y) * 1e4))
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
199 y = sort (y);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
200 end
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
201
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
202 nanidx = isnan (y);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
203 y(nanidx) = 0;
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
204 freq = zeros (n, y_nc);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
205 if (equal_bin_spacing)
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
206 if (n < 3)
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
207 d = 1;
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
208 else
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
209 d = (x(end) - x(1)) / (length (x) - 1);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
210 end
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
211 cutlen = length (cutoff);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
212 for j = 1:y_nc
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
213 freq(:,j) = accumarray (1 + max (0, min (cutlen, ceil ((double (y(:,j))
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
214 - cutoff(1)) / d))),
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
215 double (! nanidx(:,j)),
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
216 [n, 1]);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
217 end
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
218 else
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
219 for j = 1:y_nc
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
220 i = lookup (cutoff, y(:,j));
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
221 i = 1 + i - (cutoff(max (i, 1)) == y(:,j));
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
222 freq(:,j) = accumarray (i, double (! nanidx(:,j)), [n, 1]);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
223 end
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
224 end
4407
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4030
diff changeset
225 endif
16e8acbd19d5 [project @ 2003-05-05 19:00:56 by jwe]
jwe
parents: 4030
diff changeset
226
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
227 if (norm)
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
228 ## Normalize the histogram
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
229 freq .*= norm ./ sum (! nanidx);
3597
26662775f4e9 [project @ 2000-02-12 02:24:41 by jwe]
jwe
parents: 3426
diff changeset
230 endif
26662775f4e9 [project @ 2000-02-12 02:24:41 by jwe]
jwe
parents: 3426
diff changeset
231
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
232 if (nargout == 0)
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
233 if (isempty (hax))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
234 hax = gca ();
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
235 endif
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
236 bar (hax, x, freq, "hist", varargin{iarg:end});
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
237 else
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18777
diff changeset
238 if (arg_is_vector)
17487
be7e8b91c6b1 hist.m: Overhaul function.
Rik <rik@octave.org>
parents: 17162
diff changeset
239 ## Matlab compatibility requires a row vector return
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
240 nn = freq.';
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
241 xx = x.';
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
242 else
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
243 nn = freq;
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
244 xx = x;
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
245 endif
724
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
246 endif
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
247
86d73993eee2 [project @ 1994-09-21 19:42:34 by jwe]
jwe
parents:
diff changeset
248 endfunction
4811
198f3712c692 [project @ 2004-03-03 19:24:33 by jwe]
jwe
parents: 4430
diff changeset
249
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
250
4811
198f3712c692 [project @ 2004-03-03 19:24:33 by jwe]
jwe
parents: 4430
diff changeset
251 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
252 %! [nn,xx] = hist ([1:4], 3);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
253 %! assert (xx, [1.5,2.5,3.5]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
254 %! assert (nn, [2,1,1]);
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
255 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
256 %! [nn,xx] = hist ([1:4]', 3);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
257 %! assert (xx, [1.5,2.5,3.5]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
258 %! assert (nn, [2,1,1]);
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
259 %!test
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
260 %! [nn,xx] = hist ([1 1 1 NaN NaN NaN 2 2 3], [1, 2, 3]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
261 %! assert (xx, [1,2,3]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
262 %! assert (nn, [3,2,1]);
5746
e4e93452fd3e [project @ 2006-04-06 20:46:31 by jwe]
jwe
parents: 5642
diff changeset
263 %!test
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
264 %! [nn,xx] = hist ([1 1 1 NaN NaN NaN 2 2 3], [1, 2, 3], 6);
14613
e7c8e31f8e5d hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14612
diff changeset
265 %! assert (xx, [1,2,3]);
e7c8e31f8e5d hist.m: Add test to check for correct NaN normalising
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14612
diff changeset
266 %! assert (nn, [3,2,1]);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
267 %!test # Multiple columns
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
268 %! [nn,xx] = hist ([[1:4]', [1:4]'], 3);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
269 %! assert (xx, [1.5;2.5;3.5]);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
270 %! assert (nn, [[2,1,1]', [2,1,1]']);
4880
b9662e2ceb6b [project @ 2004-04-23 16:13:49 by jwe]
jwe
parents: 4873
diff changeset
271 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
272 %! for n = [10, 30, 100, 1000]
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
273 %! assert (sum (hist ([1:n], n)), n);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
274 %! assert (sum (hist ([1:n], [2:n-1])), n);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
275 %! assert (sum (hist ([1:n], [1:n])), n);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
276 %! assert (sum (hist ([1:n], 29)), n);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
277 %! assert (sum (hist ([1:n], 30)), n);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
278 %! endfor
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
279 %!assert (hist (1,1), 1)
25630
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
280 %!test <*54326> # All values identical
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
281 %! [nn,xx] = hist (ones (1,5), 3);
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
282 %! assert (nn, [0,5,0]);
b96ab99e67bb hist.m: Don't error out if called with identical Y values (bug #54326).
Sébastien Villemot <sebastien@debian.org>
parents: 25054
diff changeset
283 %! assert (xx, [0,1,2]);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
284 %!assert (size (hist (randn (750,240), 200)), [200, 240])
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
285 %!assert <*42394> (isempty (hist (rand (10,2), 0:5, 1)), false)
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
286 %!assert <*42394> (isempty (hist (rand (10,2), 0:5, [1 1])), false)
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
287
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
288 %!test <*47707>
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
289 %! y = [1 9 2 2 9 3 8 9 1 7 1 1 3 2 4 4 8 2 1 9 4 1 ...
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
290 %! 2 3 1 1 6 5 5 3 9 9 1 1 8 7 7 2 4 1];
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
291 %! [n, x] = hist (y, 10);
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
292 %! [nn, xx] = hist (uint8 (y), 10);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
293 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
294 %! assert (xx, x);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
295 %!
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
296 %! ## test again with N > 26 because there's a special case for it
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
297 %! [n, x] = hist (y, 30);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
298 %! [nn, xx] = hist (uint8 (y), 30);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
299 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
300 %! assert (xx, x);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
301
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
302 ## Test logical input
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
303 %!test
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
304 %! y = [0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0];
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
305 %! [n, x] = hist (y, 10);
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
306 %! [nn, xx] = hist (logical (y), 10);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
307 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
308 %! assert (xx, x);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
309 %!
27163
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
310 %! ## test again with N > 26 because there's a special case for it
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
311 %! [n, x] = hist (y, 30);
47c0b11da31a hist.m: Improve performance with large numbers of bins (bug #55909).
Michael Leitner <michael.leitner@frm2.tum.de>
parents: 26376
diff changeset
312 %! [nn, xx] = hist (logical (y), 30);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
313 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
314 %! assert (xx, x);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
315
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
316 ## Second output argument must be of class single if data is class single.
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
317 %!assert (class (nthargout (2, @hist, rand (10, 1, "single"))), "single")
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
318
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
319 ## Handle second argument correctly, even when it's class integer
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
320 %!test
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
321 %! y = [2.4, 2.5, 2.6, 5.4, 5.5, 5.6];
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
322 %! n = [2, 3, 1];
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
323 %! x = [1, 4, 7];
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
324 %! [nn, xx] = hist (y, uint8 ([1 4 7]));
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
325 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
326 %! assert (xx, x);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
327
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
328 ## Test bin centers
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
329 %!test
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
330 %! y = [2.4, 2.5, 2.6, 5.4, 5.5, 5.6];
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
331 %! s = (5.6 - 2.4) / 6;
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
332 %! x = [2.4+s, 4.0, 5.6-s];
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
333 %! n = [3, 0, 3];
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
334 %!
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
335 %! [nn, xx] = hist (y, 3);
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
336 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
337 %! assert (xx, x, 2*eps);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
338 %!
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
339 %! [nn, xx] = hist (y, uint8 (3));
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
340 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
341 %! assert (xx, x, 2*eps);
21627
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
342 %!
225d88a97104 hist: reduce memory usage by avoiding conversion to double (bug #47707)
Carnë Draug <carandraug@octave.org>
parents: 20852
diff changeset
343 %! [nn, xx] = hist (y, single (3));
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
344 %! assert (nn, n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
345 %! assert (xx, single (x), 2*eps ("single"));
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
346
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
347 %!test <*53199>
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
348 %! a = [ 1, 2, 3, 4, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
349 %! 5, 4, 6, 7, 8;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
350 %! 9, 12, 11, 10, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
351 %! 13, 16, 15, 14, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
352 %! 17, 20, 19, 18, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
353 %! 21, 22, 23, 2, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
354 %! 24, 27, 26, 25, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
355 %! 28, 31, 30, 29, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
356 %! 32, 35, 34, 33, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
357 %! 36, 39, 38, 37, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
358 %! 40, 43, 42, 41, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
359 %! 44, 47, 46, 45, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
360 %! 48, 51, 50, 49, 0;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
361 %! 52, 55, 54, 53, 0];
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
362 %! n = max (a(:));
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
363 %! [cnt1, ctr1] = hist(a(:), 1:n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
364 %! [cnt2, ctr2] = hist(a(:), n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
365 %! assert (cnt1, cnt2);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
366 %! assert (ctr1, 1:n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
367 %! assert (ctr2, 0.5:n);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
368
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
369 ## Test Infinite values and calculation of bins
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
370 %!test
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
371 %! y = [-Inf, NaN, 10, Inf, 0];
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
372 %! [nn, xx] = hist (y);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
373 %! assert (nn, [2 0 0 0 0 0 0 0 0 2]);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
374 %! assert (xx, 0.5:10);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
375
27164
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
376 ## Test return class of second output
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
377 %!test <*56465>
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
378 %! [nn, xx] = hist (double (1:10), single (7));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
379 %! assert (isa (xx, "double"));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
380 %! [nn, xx] = hist (single (1:10), double (7));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
381 %! assert (isa (xx, "single"));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
382 %! [nn, xx] = hist (single (1:10), double ([1, 5, 10]));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
383 %! assert (isa (xx, "double"));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
384 %! [nn, xx] = hist (double (1:10), single ([1, 5, 10]));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
385 %! assert (isa (xx, "single"));
e5f248d614f5 hist.m: Return class for second output which matches Matlab (bug #56465).
Rik <rik@octave.org>
parents: 27163
diff changeset
386
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
387 ## Test input validation
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
388 %!error hist ()
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
389 %!error <Y must be real-valued> hist (2+i)
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
390 %!error <bin specification must be a numeric> hist (1, {0,1,2})
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
391 %!error <number of bins NBINS must be positive> hist (1, 0)
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
392 %!test
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
393 %! hf = figure ("visible", "off");
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
394 %! hax = gca;
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
395 %! unwind_protect
24934
1db0b81efafe maint: strip trailing whitespace from source files
Mike Miller <mtmiller@octave.org>
parents: 24793
diff changeset
396 %! fail ("hist (hax, 1, [2 1 0])", "warning", "bin values X not sorted");
24793
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
397 %! unwind_protect_cleanup
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
398 %! close (hf);
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
399 %! end_unwind_protect
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
400 %!error <bin specification must be a scalar or vector> hist (1, ones (2,2))
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
401 %!error <NORM must be a numeric constant> hist (1,1, {1})
1fa1869650cc hist.m: Overhaul function (bug #53199).
Rik <rik@octave.org>
parents: 24534
diff changeset
402 %!error <NORM must be a numeric constant . 0> hist (1,1, -1)