annotate scripts/statistics/mean.m @ 28929:9e43deb9bfc3

maint: Use semicolon after assert statement inside %!test blocks. * interpft.m, ind2rgb.m, null.m, inputParser.m, inputname.m, ode15i.m, ode15s.m, ode23.m, ode23s.m, ode45.m, pathdef.m, camlookat.m, camzoom.m, material.m, deconv.m, ismember.m, eigs.m, gmres.m, ilu.m, tfqmr.m, perms.m, sinint.m, gallery.m, mean.m: maint: Use semicolon after assert statement inside %!test blocks.
author Rik <rik@octave.org>
date Wed, 14 Oct 2020 11:46:55 -0700
parents b09432b20a84
children 6e460773bdda
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) 1995-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26596
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: 3367
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: 24512
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: 24512
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: 3367
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: 3367
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: 7001
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: 24512
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
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
27 ## @deftypefn {} {} mean (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
28 ## @deftypefnx {} {} mean (@var{x}, @var{dim})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
29 ## @deftypefnx {} {} mean (@var{x}, @var{opt})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
30 ## @deftypefnx {} {} mean (@var{x}, @var{dim}, @var{opt})
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
31 ## @deftypefnx {} {} mean (@dots{}, @var{outtype})
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
32 ## Compute the mean of the elements of the vector @var{x}.
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ##
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## The mean is defined as
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
36 ## @tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
37 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$
24511
4f0e6ee6c9b8 Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 23219
diff changeset
38 ## where $N$ is the number of elements of @var{x}.
4f0e6ee6c9b8 Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 23219
diff changeset
39 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
40 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
41 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
42 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
43 ## @example
24511
4f0e6ee6c9b8 Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 23219
diff changeset
44 ## mean (@var{x}) = SUM_i @var{x}(i) / N
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
45 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
46 ##
24985
d85b2485af9e doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24869
diff changeset
47 ## @noindent
24511
4f0e6ee6c9b8 Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 23219
diff changeset
48 ## where @math{N} is the length of the @var{x} vector.
4f0e6ee6c9b8 Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents: 23219
diff changeset
49 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
50 ## @end ifnottex
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
51 ## If @var{x} is a matrix, compute the mean for each column and return them
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
52 ## in a row vector.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
53 ##
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
54 ## If the optional argument @var{dim} is given, operate along this dimension.
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
55 ##
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
56 ## The optional argument @var{opt} selects the type of mean to compute.
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
57 ## The following options are recognized:
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
58 ##
11595
5ec6aa05638d Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
59 ## @table @asis
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15467
diff changeset
60 ## @item @qcode{"a"}
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
61 ## Compute the (ordinary) arithmetic mean. [default]
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
62 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15467
diff changeset
63 ## @item @qcode{"g"}
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6046
diff changeset
64 ## Compute the geometric mean.
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
65 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15467
diff changeset
66 ## @item @qcode{"h"}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
67 ## Compute the harmonic mean.
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
68 ## @end table
4022
789c4112a37d [project @ 2002-08-06 03:42:41 by jwe]
jwe
parents: 3922
diff changeset
69 ##
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
70 ## The optional argument @var{outtype} selects the data type of the
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
71 ## output value. The following options are recognized:
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
72 ##
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
73 ## @table @asis
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
74 ## @item @qcode{"default"}
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
75 ## Output will be of class double unless @var{x} is of class single,
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
76 ## in which case the output will also be single.
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
77 ##
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
78 ## @item @qcode{"double"}
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
79 ## Output will be of class double.
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
80 ##
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
81 ## @item @qcode{"native"}
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
82 ## Output will be the same class as @var{x} unless @var{x} is of class
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
83 ## logical in which case it returns of class double.
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
84 ##
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
85 ## @end table
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
86 ##
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
87 ## Both @var{dim} and @var{opt} are optional. If both are supplied, either
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
88 ## may appear first.
12575
d0b799dafede Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents: 12454
diff changeset
89 ## @seealso{median, mode}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
90 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
91
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
92 function y = mean (x, varargin)
4022
789c4112a37d [project @ 2002-08-06 03:42:41 by jwe]
jwe
parents: 3922
diff changeset
93
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
94 if (nargin < 1 || nargin > 4)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
95 print_usage ();
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
96 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
97
12435
0f21f258aa17 mean: also accept logical values
John W. Eaton <jwe@octave.org>
parents: 11595
diff changeset
98 if (! (isnumeric (x) || islogical (x)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
99 error ("mean: X must be a numeric vector or matrix");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
100 endif
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
101 nd = ndims (x);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
102 sz = size (x);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
103
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
104 ## We support too many options...
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
105
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
106 ## If OUTTYPE is set, it must be the last option. If DIM and
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
107 ## MEAN_TYPE exist, they must be the first two options
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
108
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
109 out_type = "default";
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
110 if (numel (varargin))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
111 maybe_out_type = tolower (varargin{end});
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
112 if (any (strcmpi (maybe_out_type, {"default", "double", "native"})))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
113 out_type = maybe_out_type;
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
114 varargin(end) = [];
4022
789c4112a37d [project @ 2002-08-06 03:42:41 by jwe]
jwe
parents: 3922
diff changeset
115 endif
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
116 endif
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
117
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
118 scalars = cellfun (@isscalar, varargin);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
119 chars = cellfun (@ischar, varargin);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
120 numerics = cellfun (@isnumeric, varargin);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
121
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
122 dim_mask = numerics & scalars;
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
123 mean_type_mask = chars & scalars;
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
124 if (! all (dim_mask | mean_type_mask))
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5443
diff changeset
125 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
126 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
127
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
128 switch (nnz (dim_mask))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
129 case 0 # Find the first non-singleton dimension
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
130 (dim = find (sz > 1, 1)) || (dim = 1);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
131 case 1
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
132 dim = varargin{dim_mask};
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
133 if (dim != fix (dim) || dim < 1)
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
134 error ("mean: DIM must be an integer and a valid dimension");
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
135 endif
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
136 otherwise
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
137 print_usage ();
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
138 endswitch
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
139
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
140 switch (nnz (mean_type_mask))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
141 case 0
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
142 mean_type = "a";
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
143 case 1
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
144 mean_type = varargin{mean_type_mask};
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
145 otherwise
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
146 print_usage ();
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
147 endswitch
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
148
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
149 ## The actual mean computation
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
150 n = size (x, dim);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
151 switch (mean_type)
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
152 case "a"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
153 y = sum (x, dim) / n;
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
154 case "g"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
155 if (all (x(:) >= 0))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
156 y = exp (sum (log (x), dim) ./ n);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
157 else
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
158 error ("mean: X must not contain any negative values");
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
159 endif
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
160 case "h"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
161 y = n ./ sum (1 ./ x, dim);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
162 otherwise
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
163 error ("mean: mean type '%s' not recognized", mean_type);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
164 endswitch
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
165
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
166 ## Convert output as requested
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
167 switch (out_type)
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
168 case "default"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
169 ## do nothing, the operators already do the right thing
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
170 case "double"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
171 y = double (y);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
172 case "native"
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
173 if (islogical (x))
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
174 ## ignore it, return double anyway
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
175 else
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
176 y = cast (y, class (x));
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
177 endif
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
178 otherwise
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
179 ## this should have been filtered out during input check, but...
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
180 error ("mean: OUTTYPE '%s' not recognized", out_type);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
181 endswitch
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
182 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
183
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
184
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
185 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
186 %! x = -10:10;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
187 %! y = x';
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
188 %! z = [y, y+10];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
189 %! assert (mean (x), 0);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
190 %! assert (mean (y), 0);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
191 %! assert (mean (z), [0, 10]);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
192
15464
930117c97760 mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14868
diff changeset
193 ## Test small numbers
930117c97760 mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14868
diff changeset
194 %!assert (mean (repmat (0.1,1,1000), "g"), 0.1, 20*eps)
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
195
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
196 %!assert (mean (magic (3), 1), [5, 5, 5])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
197 %!assert (mean (magic (3), 2), [5; 5; 5])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
198 %!assert (mean ([2 8], "g"), 4)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
199 %!assert (mean ([4 4 2], "h"), 3)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
200 %!assert (mean (logical ([1 0 1 1])), 0.75)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
201 %!assert (mean (single ([1 0 1 1])), single (0.75))
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
202 %!assert (mean ([1 2], 3), [1 2])
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
203
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
204 ## Test input validation
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
205 %!error <Invalid call to mean. Correct usage is> mean ()
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
206 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3, 4)
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
207 %!error <X must be a numeric> mean ({1:5})
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
208 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3)
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
209 %!error <Invalid call to mean. Correct usage is> mean (1, ones (2,2))
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
210 %!error <DIM must be an integer> mean (1, 1.5)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
211 %!error <DIM must be .* a valid dimension> mean (1, 0)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
212 %!error <X must not contain any negative values> mean ([1 -1], "g")
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
213 %!error <mean type 'b' not recognized> mean (1, "b")
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
214 %!error <Invalid call to mean. Correct usage is> mean (1, 1, "foo")
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
215
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
216 ## Test outtype option
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
217 %!test
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
218 %! in = [1 2 3];
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
219 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
220 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
221 %! assert (mean (in, "default"), out);
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
222 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
223 %! in = single ([1 2 3]);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
224 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
225 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
226 %! assert (mean (in, "default"), single (out));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
227 %! assert (mean (in, "double"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
228 %! assert (mean (in, "native"), single (out));
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
229 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
230 %! in = uint8 ([1 2 3]);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
231 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
232 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
233 %! assert (mean (in, "default"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
234 %! assert (mean (in, "double"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
235 %! assert (mean (in, "native"), uint8 (out));
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
236 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
237 %! in = logical ([1 0 1]);
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
238 %! out = 2/3;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
239 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
240 %! assert (mean (in, "default"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
241 %! assert (mean (in, "native"), out); # logical ignores native option