annotate scripts/statistics/iqr.m @ 33625:d213a148b3f1 default tip @

ensure exp. terminal widget has focus at startup * main-window.cc (main_window): call focus_command_window only if event loop is idle by using a single shot timer * main-window.h: make focus_command_window a public slot
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 26 May 2024 02:29:44 +0200
parents 95c195edc257
children
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 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 32002
diff changeset
3 ## Copyright (C) 1995-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
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/>.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
7 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
8 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
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: 23220
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: 22323
diff changeset
13 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
19 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
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: 6046
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: 23220
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 ########################################################################
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
25
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
26 ## -*- texinfo -*-
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30330
diff changeset
27 ## @deftypefn {} {@var{Z} =} iqr (@var{x})
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
28 ## @deftypefnx {} {@var{Z} =} iqr (@var{x}, @var{dim})
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
29 ## @deftypefnx {} {@var{Z} =} iqr (@var{x}, @qcode{"ALL"})
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
30 ## Return the interquartile range of @var{x}, defined as the distance between
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
31 ## the 25th and 75th percentile values of @var{x} calculated using:
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
32 ## quantile (x, [0.25 0.75])
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ##
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
34 ## If @var{x} is a vector, @code{iqr (@var{x})} will operate on the data in
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## @var{x}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
36 ##
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
37 ## If @var{x} is a matrix, @code{iqr (@var{x})} will operate independently on
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
38 ## each column in @var{x} returning a row vector @var{Z}.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
39 ##
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
40 ## If @var{x} is a n-dimensional array, @code{iqr (@var{x})} will operate
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
41 ## independently on the first non-singleton dimension in @var{x}, returning an
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
42 ## array @var{Z} the same shape as @var{x} with the non-singleton dimenion
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
43 ## reduced to 1.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
44 ##
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
45 ## The optional variable @var{dim} can be used to force @code{iqr} to operate
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
46 ## over the specified dimension. @var{dim} can either be a scalar dimension or
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
47 ## a vector of non-repeating dimensions over which to operate. In either case
30353
212461a26e9c doc: grammarcheck m-files in scripts/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30330
diff changeset
48 ## @var{dim} must be positive integers. A vector @var{dim} concatenates all
30371
1c114a55f4fb doc: Spellcheck documentation ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30353
diff changeset
49 ## specified dimensions for independent operation by @code{iqr}.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
50 ##
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
51 ## Specifying dimension @qcode{"ALL"} will force @code{iqr} to operate
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
52 ## on all elements of @var{x}, and is equivalent to @code{iqr (@var{x}(:))}.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
53 ## Similarly, specifying a vector dimension including all non-singleton
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
54 ## dimensions of @var{x} is equivalent to @code{iqr (@var{x}, @qcode{"ALL"})}.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
55 ##
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
56 ## If @var{x} is a scalar, or only singleton dimensions are specified for
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
57 ## @var{dim}, the output will be @code{zeros (size (@var{x}))}.
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
58 ##
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
59 ## As a measure of dispersion, the interquartile range is less affected by
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
60 ## outliers than either @code{range} or @code{std}.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
61 ##
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
62 ## @seealso{bounds, mad, range, std, prctile, quantile}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
63 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
64
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
65 ## TODO: When Probability Distribution Objects are implemented, enable
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
66 ## handling for those object types.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
67
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
68 function z = iqr (x, dim)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
69
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
70 ## Perform input checks.
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
71 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5435
diff changeset
72 print_usage ();
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
73 elseif (nargin < 2)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
74 dim = [];
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
75 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
76
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
77 vecdim_flag = false;
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4681
diff changeset
78 nd = ndims (x);
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4681
diff changeset
79 sz = size (x);
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
80 empty_x = any (sz == 0);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
81
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
82 if (! (empty_x || isnumeric (x) || islogical (x)))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
83 error ("iqr: X must be a numeric vector or matrix");
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
84 endif
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
85
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30371
diff changeset
86 if (isempty (dim))
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
87 ## Find first non-singleton dimension.
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
88 if (max (sz) == 1 && ! empty_x)
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
89 dim = 2;
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
90 else
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
91 dim = find ((sz != 1), 1);
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
92 endif
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
93
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
94 if (empty_x)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
95 ## Handle empty x with no input dim.
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
96 if ((ndims (x) == 2 && all (sz == 0)) || iscolumn (x) || isrow (x))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
97 z = NaN;
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
98 else
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
99 sz(dim) = 1;
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
100 z = NaN (sz);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
101 endif
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
102 return
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
103 endif
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
104
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4681
diff changeset
105 else
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
106
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29772
diff changeset
107 if (isvector (dim) && isnumeric (dim)
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29772
diff changeset
108 && all (dim > 0) && all (rem (dim, 1) == 0))
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
109
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29772
diff changeset
110 if (((num_vecdims = numel (dim)) > 1) && all (diff (sort (dim))))
31253
a40c0b7aa376 maint: changes to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 30564
diff changeset
111 ## DIM must be 1-D and non repeating.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
112
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
113 if (empty_x)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
114 ## Handle empty x with input vecdim.
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
115 sz(dim(dim <= nd)) = 1;
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
116 z = NaN (sz);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
117 return
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
118 endif
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
119
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
120 ## Detect trivial case of DIM being all dimensions (same as "all").
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
121 highest_dim = (max (nd, max (dim)));
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
122 if ((num_vecdims == nd) && (highest_dim == nd))
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
123 x = x(:);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
124 sz = size (x);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
125 dim = 1;
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
126 else
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
127 ## Move dimensions for operation to the front, keeping the order of
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
128 ## the remaining dimensions.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
129 ## Reshape those into a single dimension.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
130 ## Process as normal for a dim1 iqr on X, reshape when done.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
131
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
132 vecdim_flag = true; ## flag for final reshape
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
133
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
134 if (iscolumn (dim))
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
135 dim = dim.';
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
136 endif
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
137
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
138 ## Permutation vector with DIM at front
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
139 perm = [1:highest_dim];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
140 perm(dim) = [];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
141 perm = [dim, perm];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
142
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
143 ## Reshape X to put dims to process at front.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
144 x = permute (x, perm);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
145 sz_x_new = size (x);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
146
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
147 ## Preserve trailing singletons when dim > ndims (x).
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
148 sz_x_new = [sz_x_new, ones(1, highest_dim - numel (sz_x_new))];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
149
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
150 newshape = [prod(sz_x_new(1:num_vecdims)), ...
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
151 ones(1, (num_vecdims-1)), ...
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
152 sz_x_new((num_vecdims+1):end)];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
153
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
154 if (numel (newshape) == 1)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
155 newshape = [newshape, 1];
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
156 endif
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
157
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
158 ## Collapse dimensions to be processses into single column.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
159 x = reshape (x, newshape);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
160
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
161 ## Operate column-wise.
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
162 dim = 1;
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
163 endif
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
164
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
165 elseif (! isscalar (dim))
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
166 error ("iqr: vector DIM must contain non-repeating positive integers");
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
167
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
168 elseif (empty_x)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
169 ## Handle empty x with scalar input dim.
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
170 sz(dim(dim <= nd)) = 1;
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
171 z = NaN (sz);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
172 return
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
173 endif
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
174
32002
939e5d952675 Use upper/lower in preference to toupper/tolower to match Matlab.
Rik <rik@octave.org>
parents: 31706
diff changeset
175 elseif (strcmp (lower (dim), "all"))
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
176 ## "ALL" simplifies to collapsing all elements to single vector.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
177 x = x(:);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
178 dim = 1;
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
179 sz = size (x);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
180
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
181 if (empty_x)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
182 ## Handle empty x with "all" dim input.
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
183 z = NaN;
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
184 return
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
185 endif
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
186
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
187 else
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
188 error ("iqr: DIM must be a positive integer scalar, vector, or 'all'");
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4681
diff changeset
189 endif
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
190
4681
bb9515a3527d [project @ 2003-12-18 15:35:09 by jwe]
jwe
parents: 4631
diff changeset
191 endif
4631
cae0b7c46842 [project @ 2003-11-18 18:30:06 by jwe]
jwe
parents: 3922
diff changeset
192
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
193 if (((dim > nd) || (sz(dim) == 1)) && all (isfinite (x)))
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
194 ## Shortcut easy zeros.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
195 z = zeros (sz);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
196 elseif (iscolumn (x) && (dim == 1))
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
197 ## Detect col vector with quantile/diff dim requirement mismatch.
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
198 z = abs (diff (quantile (x, [0.25, 0.75], 1), [], 2));
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
199 else
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
200 z = abs (diff (quantile (x, [0.25, 0.75], dim), [], dim));
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
201 endif
4631
cae0b7c46842 [project @ 2003-11-18 18:30:06 by jwe]
jwe
parents: 3922
diff changeset
202
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
203 if (vecdim_flag)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
204 z = ipermute (z, perm);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
205 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
206
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
207 endfunction
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
208
30379
363fb10055df maint: Style check m-files ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30371
diff changeset
209
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
210 %!assert (iqr (17), 0)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
211 %!assert (iqr (17, 1), 0)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
212 %!assert (iqr (17, 4), 0)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
213 %!assert (iqr (1:3), 1.5)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
214 %!assert (iqr (1:4), 2)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
215 %!assert (iqr (1:5), 2.5)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
216 %!assert (iqr (1:10), 5)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
217 %!assert (iqr ((1:10).'), 5)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
218 %!assert (iqr (1:10, 2), 5)
30383
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30379
diff changeset
219 %!assert (iqr (1:10, 1), zeros (1, 10))
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30379
diff changeset
220 %!assert (iqr (1:10, 3), zeros (1, 10))
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
221 %!assert (iqr ([1:5; 2:6], "all"), 3)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
222
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
223 %!test
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
224 %! x = reshape (1:6, [1, 2, 3]);
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
225 %! assert (iqr (x), ones (1, 1, 3));
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
226 %! assert (iqr (x, 1), zeros (1, 2, 3));
30383
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30379
diff changeset
227 %! assert (iqr (x, 2), ones (1, 1, 3));
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
228 %! assert (iqr (x, 3), [3, 3]);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
229
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
230 ## n-D arrays
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
231 %!test
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
232 %! x = magic (4); x = cat (3,x, 2*x, 3*x); x = cat (4, x, 2*x);
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
233 %! y = cat (3, 8*[1, 1, 1, 1], 16*[1, 1, 1, 1], 24*[1, 1, 1, 1]);
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
234 %! assert (iqr (x), cat (4, y, 2*y));
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
235 %! assert (iqr (x, 1), cat (4, y, 2*y));
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
236 %! y = cat (3, 4*[3, 1, 1, 3].', 8*[3, 1, 1, 3].', 12*[3, 1, 1, 3].');
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
237 %! assert (iqr (x, 2), cat (4, y, 2*y));
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
238 %! y = [24, 3, 4.5, 19.5; 7.5, 16.5, 15, 12; 13.5, 10.5, 9, 18; 6, 21, 22.5, 1.5];
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
239 %! assert (iqr (x, 3), cat (4, y, 2*y));
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
240 %! y = [16, 2, 3, 13; 5, 11, 10, 8; 9, 7, 6, 12; 4, 14, 15, 1];
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
241 %! assert (iqr (x, 4), cat (3, y, 2*y, 3*y));
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
242 %! assert (iqr (x, 5), zeros (size (x)));
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
243
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
244 ## vector dimensions
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
245 %!assert (iqr (17, [1, 8]), 0)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
246 %!assert (iqr ([[1, 2, 5]; [2, 5, 6]], [1, 2]), 3)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
247 %!assert (iqr (cat (3, [1, 2, 5; 2, 5, 6], [1, 2, 5; 2, 5, 6]), [1, 2]), cat(3, 3, 3))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
248 %!assert (iqr (cat (3, [1, 2, 5; 2, 5, 6], [1, 2, 5; 2, 5, 6]), [1, 2]'), cat(3, 3, 3))
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
249 %!test
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
250 %! x = magic (4); x = cat (3, x, 2*x, 3*x); x = cat (4, x, 2*x);
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
251 %! y = cat (3, 8, 16, 24);
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
252 %! assert (iqr (x, [1, 2]), cat (4, y, 2*y));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
253 %! y = [14, 18.5, 17.5, 19.5];
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
254 %! assert (iqr (x, [1, 3]), cat (4, y, 2*y));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
255 %! y = [10.5, 12.5, 11.5, 15];
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
256 %! assert (iqr (x, [1, 4]), cat (3, y, 2*y, 3*y));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
257 %! assert (iqr (x, [1, 5]), iqr (x, 1));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
258 %! y = [24, 13, 12, 25.5]';
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
259 %! assert (iqr (x, [2, 3]), cat (4, y, 2*y));
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
260 %! y = [17.5, 9, 8, 18.5]';
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
261 %! assert (iqr (x, [2, 4]), cat (3, y, 2*y, 3*y));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
262 %! assert (iqr (x, [3, 4]), [32, 4, 6, 26; 10, 22, 20, 16; 18, 14, 12, 24; 8, 28, 30, 2]);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
263 %! assert (iqr (x, [3, 4]), iqr (x, [4, 3]));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
264 %! assert (iqr (x, [1, 2, 3]), cat (4, 17.5, 35));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
265 %! assert (iqr (x, [2, 3, 4]), [29.5, 19.5, 23, 31]');
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
266 %! assert (iqr (x, [1, 3, 4]), [22, 28, 22, 30.5]);
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
267 %! assert (iqr (x, [1, 2, 4]), cat (3, 11, 22, 33));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
268 %! assert (iqr (x, [1, 2, 5]), iqr (x, [1, 2]));
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
269 %! assert (iqr (x, [5, 6]), zeros (size (x)));
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
270
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
271 ## Inf, NaN
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
272 %!assert (iqr (Inf), NaN)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
273 %!assert (iqr (-Inf), NaN)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
274 %!assert (iqr (NaN), NaN)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
275 %!assert (iqr (NaN), NaN)
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
276 %!assert (iqr ([1, 2, Inf], 1), [0, 0, NaN])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
277 %!assert (iqr ([1, 2, Inf], 2), Inf)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
278 %!assert (iqr ([1, 2, -Inf], 1), [0, 0, NaN])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
279 %!assert (iqr ([1, 2, -Inf], 2), Inf)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
280 %!assert (iqr ([1, 2, 3, NaN], 1), [0, 0, 0, NaN])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
281 %!assert (iqr ([1, 2, 3, NaN], 2), 1.5)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
282 %!assert (iqr ([1, NaN, 2, 3], 2), 1.5)
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
283 %!assert (iqr (NaN (2), 1), [NaN, NaN])
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
284 %!assert (iqr (NaN (2), 2), [NaN; NaN])
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
285 %!assert (iqr (NaN (2), 3), NaN (2))
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
286 %!assert (iqr ([[1, 2, 5], [2, NaN, 6]], "all"), 3.5)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
287
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
288 ## Empty inputs
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
289 %!assert <*65531> (iqr ([]), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
290 %!assert <*65531> (iqr (ones (0, 1)), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
291 %!assert <*65531> (iqr (ones (0, 1), 1), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
292 %!assert <*65531> (iqr (ones (0, 1), 2), NaN (0, 1))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
293 %!assert <*65531> (iqr (ones (0, 1), 3), NaN (0, 1))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
294 %!assert <*65531> (iqr (ones (1, 0)), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
295 %!assert <*65531> (iqr (ones (1, 0), 1), NaN (1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
296 %!assert <*65531> (iqr (ones (1, 0), 2), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
297 %!assert <*65531> (iqr (ones (1, 0), 3), NaN (1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
298 %!assert <*65531> (iqr (ones (1, 0), 9), NaN (1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
299 %!assert <*65531> (iqr (ones (1, 1, 0)), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
300 %!assert <*65531> (iqr (ones (0, 0, 1, 0)), NaN (1, 0, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
301 %!assert <*65531> (iqr (ones (1, 1, 1, 0)), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
302 %!assert <*65531> (iqr (ones (1, 1, 1, 0), 1), NaN (1, 1, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
303 %!assert <*65531> (iqr (ones (1, 1, 1, 0), 1), NaN (1, 1, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
304 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 1), NaN (1, 0, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
305 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 2), NaN (0, 1, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
306 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 3), NaN (0, 0, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
307 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 4), NaN (0, 0, 1, 1))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
308 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 9), NaN (0, 0, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
309 %!assert <*65531> (iqr (ones (0, 0, 1, 0), [1, 2]), NaN (1, 1, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
310 %!assert <*65531> (iqr (ones (0, 0, 1, 0), [1, 4]), NaN (1, 0, 1, 1))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
311 %!assert <*65531> (iqr (ones (0, 0, 1, 0), [1, 9]), NaN (1, 0, 1, 0))
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
312 %!assert <*65531> (iqr ([], "all"), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
313 %!assert <*65531> (iqr (ones (0, 1), "all"), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
314 %!assert <*65531> (iqr (ones (1, 0), "all"), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
315 %!assert <*65531> (iqr (ones (1, 1, 0), "all"), NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
316 %!assert <*65531> (iqr (ones (0, 0, 1, 0), 'all'), NaN)
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
317
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
318 ## input validation
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
319 %!error <Invalid call> iqr ()
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
320 %!error iqr (1, 2, 3)
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
321 %!error <X must be a numeric> iqr (['A'; 'B'])
29772
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
322 %!error <DIM .* positive integer> iqr (1, 'A')
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
323 %!error <DIM .* positive integer> iqr (1, 0)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
324 %!error <DIM .* positive integer> iqr (1, -2)
fdbba73edde2 iqr.m: Overhaul function (bug #59636).
Nicholas R. Jankowski <jankowskin@asme.org>
parents: 29359
diff changeset
325 %!error <DIM .* positive integer> iqr (1, 1.4)
33267
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
326 %!error <DIM .* positive integer> iqr (1, [1, -2])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
327 %!error <DIM .* positive integer> iqr (1, [1, 1.4])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
328 %!error <DIM .* positive integer> iqr ([1, 2, 3], NaN)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
329 %!error <DIM .* positive integer> iqr ([1, 2, 3], [2, NaN])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
330 %!error <DIM .* positive integer> iqr ([1, 2, 3], Inf)
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
331 %!error <DIM .* positive integer> iqr ([1, 2, 3], [2, Inf])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
332 %!error <vector DIM .* non-repeating> iqr ([1, 2, 3], [1, 2, 1])
95c195edc257 iqr: Produce compatible output for empty x inputs (bug #65531)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents: 32632
diff changeset
333 %!error <DIM .* vector> iqr (1, [1, 2; 3, 4])