annotate scripts/statistics/mean.m @ 31511:9ee8943cad01 stable

mean.m: Use Octave coding conventions. * mean.m: Improve DOCSTRING to follow conventions in other statistics functions. Use numel() in place of length(). Use temporary variable idx to capture results of call to isnan() rather than calling isnan() twice on potentially large inputs. Use standard code for finding first non-singleton dimension. Use input parameter names from DOCSTRING in error() messages. Place input validation BIST tests at end of function.
author Rik <rik@octave.org>
date Tue, 22 Nov 2022 16:46:35 -0800
parents 21962c678648
children 24ca5cb70a4a
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 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 1995-2022 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 -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{y} =} mean (@var{x})
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
28 ## @deftypefnx {} {@var{y} =} mean (@var{x}, 'all')
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
29 ## @deftypefnx {} {@var{y} =} mean (@var{x}, @var{dim})
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
30 ## @deftypefnx {} {@var{y} =} mean (@dots{}, '@var{outtype}')
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
31 ## @deftypefnx {} {@var{y} =} mean (@dots{}, '@var{nanflag}')
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
32 ## Compute the mean of the elements of @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 ##
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
34 ## @itemize
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
35 ## @item
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
36 ## If @var{x} is a vector, then @code{mean (@var{x})} returns the
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
37 ## mean of the elements in @var{x} defined as
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
38 ## @tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
39 ## $$ {\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
40 ## 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
41 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
42 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
43 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
44 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
45 ## @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
46 ## 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
47 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
48 ##
24985
d85b2485af9e doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24869
diff changeset
49 ## @noindent
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
50 ## where @math{N} is the number of elements in the @var{x} vector.
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
51 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7411
diff changeset
52 ## @end ifnottex
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
53 ##
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
54 ## @item
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
55 ## If @var{x} is a matrix, then @code{mean} returns a row vector with the mean
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
56 ## of each column in @var{x}.
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
57 ##
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
58 ## @item
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
59 ## If @var{x} is a multi-dimensional array, then @code{mean} operates along the
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
60 ## first non-singleton dimension of @var{x}.
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
61 ## @end itemize
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
62 ##
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
63 ## The optional input @var{dim} forces @code{mean} to operate over the
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
64 ## specified dimension(s). @var{dim} can either be a scalar dimension or a
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
65 ## vector of non-repeating dimensions. Dimensions must be positive integers,
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
66 ## and the mean is calculated over the array slice defined by @var{dim}.
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
67 ##
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
68 ## Specifying dimension @qcode{"all"} will force @code{mean} to operate on all
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
69 ## elements of @var{x}, and is equivalent to @code{mean (@var{x}(:))}.
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
70 ##
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
71 ## The optional input @var{outtype} specifies the data type that is returned.
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
72 ## Valid values are:
4022
789c4112a37d [project @ 2002-08-06 03:42:41 by jwe]
jwe
parents: 3922
diff changeset
73 ##
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
74 ## @table @asis
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
75 ## @item @qcode{'default'} : Output is of type double, unless the input is
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
76 ## single in which case the output is of type single.
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
77 ##
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
78 ## @item @qcode{'double'} : Output is of type double.
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
79 ##
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
80 ## @item @qcode{'native'} : Output is of the same type as the input
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
81 ## (@code{class (@var{x})}), unless the input is logical in which case the
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
82 ## output is of type double.
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
83 ##
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
84 ## @end table
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
85 ##
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
86 ## The optional input @var{nanflag} specifies whether to include/exclude NaN
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
87 ## values from the calculation. By default, NaN values are included in the
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
88 ## calculation (@var{nanflag} has the value @qcode{'includenan'}). To exclude
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
89 ## NaN values, set the value of @var{nanflag} to @qcode{'omitnan'}.
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
90 ##
12575
d0b799dafede Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents: 12454
diff changeset
91 ## @seealso{median, mode}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3265
diff changeset
92 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
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 function y = mean (x, varargin)
4022
789c4112a37d [project @ 2002-08-06 03:42:41 by jwe]
jwe
parents: 3922
diff changeset
95
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
96 if (nargin < 1 || nargin > 4 || ! all (cellfun (@ischar, varargin(2:end))))
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
97 print_usage ();
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
98 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
99
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
100 ## Check all char arguments.
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
101 all_flag = false;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
102 omitnan = false;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
103 outtype = "default";
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
104
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
105 for i = 1:numel (varargin)
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
106 if (ischar (varargin{i}))
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
107 switch (varargin{i})
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
108 case "all"
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
109 all_flag = true;
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
110 case "includenan"
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
111 omitnan = false;
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
112 case "omitnan"
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
113 omitnan = true;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
114 case {"default", "double", "native"}
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
115 outtype = varargin{i};
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
116 otherwise
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
117 print_usage ();
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
118 endswitch
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
119 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
120 endfor
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
121 varargin(cellfun (@ischar, varargin)) = [];
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
122
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
123 if (((numel (varargin) == 1) && ! (isnumeric (varargin{1}))) ...
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
124 || (numel (varargin) > 1))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
125 print_usage ();
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
126 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
127
12435
0f21f258aa17 mean: also accept logical values
John W. Eaton <jwe@octave.org>
parents: 11595
diff changeset
128 if (! (isnumeric (x) || islogical (x)))
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
129 error ("mean: X must be either a numeric or logical vector or matrix");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
130 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
131
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
132 if (numel (varargin) == 0)
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
133
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
134 ## Single numeric input argument, no dimensions given.
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
135 if (all_flag)
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
136 n = numel (x(:));
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
137 if (omitnan)
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
138 idx = isnan (x);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
139 n -= sum (idx(:));
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
140 x(idx) = 0;
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
141 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
142 y = sum (x(:), 1) ./ n;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
143 else
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
144 sz = size (x);
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
145 ## Find the first non-singleton dimension.
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
146 (dim = find (sz != 1, 1)) || (dim = 1);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
147 n = size (x, dim);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
148 if (omitnan)
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
149 idx = isnan (x);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
150 n = sum (! idx, dim);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
151 x(idx) = 0;
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
152 endif
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
153 y = sum (x, dim) ./ n;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
154 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
155
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
156 else
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
157
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
158 ## Two numeric input arguments, dimensions given. Note scalar is vector!
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
159 dim = varargin{1};
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
160 if (! (isvector (dim) && all (dim)) || any (rem (dim, 1)))
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
161 error ("mean: DIM must be a positive integer scalar or vector");
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
162 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
163
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
164 if (isscalar (dim))
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
165
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
166 n = size (x, dim);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
167 if (omitnan)
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
168 idx = isnan (x);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
169 n = sum (! idx, dim);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
170 x(idx) = 0;
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
171 endif
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
172 y = sum (x, dim) ./ n;
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
173
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
174 else
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
175
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
176 sz = size (x);
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
177 ndims = numel (sz);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
178 misdim = [1:ndims];
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
179
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
180 ## keep remaining dimensions
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
181 for i = 1:numel (dim)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
182 misdim(misdim == dim(i)) = [];
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
183 endfor
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
184
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
185 switch (numel (misdim))
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
186 ## if all dimensions are given, compute x(:)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
187 case 0
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
188 n = numel (x(:));
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
189 if (omitnan)
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
190 idx = isnan (x);
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
191 n -= sum (idx(:));
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
192 x(idx) = 0;
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
193 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
194 y = sum (x(:), 1) ./ n;
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
195
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
196 ## for 1 dimension left, return column vector
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
197 case 1
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
198 x = permute (x, [misdim, dim]);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
199 for i = 1:size (x, 1)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
200 x_vec = x(i,:,:,:,:,:,:)(:);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
201 if (omitnan)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
202 x_vec = x_vec(! isnan (x_vec));
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
203 endif
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
204 y(i) = sum (x_vec, 1) ./ numel (x_vec);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
205 endfor
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
206
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
207 ## for 2 dimensions left, return matrix
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
208 case 2
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
209 x = permute (x, [misdim, dim]);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
210 for i = 1:size (x, 1)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
211 for j = 1:size (x, 2)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
212 x_vec = x(i,j,:,:,:,:,:)(:);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
213 if (omitnan)
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
214 x_vec = x_vec(! isnan (x_vec));
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
215 endif
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
216 y(i,j) = sum (x_vec, 1) ./ numel (x_vec);
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
217 endfor
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
218 endfor
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
219
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
220 ## for more than 2 dimensions left, throw error
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
221 otherwise
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
222 error ("DIM must index at least N-2 dimensions of X");
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
223 endswitch
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
224 endif
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
225
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
226 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
227
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
228 ## Convert output if requested
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
229 switch (outtype)
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
230 case "default"
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
231 ## do nothing, the operators already do the right thing.
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
232 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
233 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
234 case "native"
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
235 if (! islogical (x))
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 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
237 endif
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
238 otherwise
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
239 ## FIXME: This is unreachable code. Valid values already
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
240 ## checked in input validation.
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
241 error ("mean: OUTTYPE '%s' not recognized", outtype);
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
242 endswitch
28945
6e460773bdda maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents: 28929
diff changeset
243
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
244 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
245
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
246
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
247 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
248 %! x = -10:10;
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
249 %! y = x';
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
250 %! 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
251 %! assert (mean (x), 0);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
252 %! assert (mean (y), 0);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
253 %! assert (mean (z), [0, 10]);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
254
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
255 %!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
256 %!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
257 %!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
258 %!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
259 %!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
260
24869
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
261 ## 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
262 %!test
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
263 %! 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
264 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
265 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
266 %! 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
267 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
268 %! 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
269 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
270 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
271 %! assert (mean (in, "default"), single (out));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
272 %! assert (mean (in, "double"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
273 %! 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
274 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
275 %! 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
276 %! out = 2;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
277 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
278 %! assert (mean (in, "default"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
279 %! assert (mean (in, "double"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
280 %! 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
281 %!
170e8625562a mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents: 24547
diff changeset
282 %! 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
283 %! out = 2/3;
28929
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
284 %! assert (mean (in, "default"), mean (in));
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
285 %! assert (mean (in, "default"), out);
9e43deb9bfc3 maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents: 27984
diff changeset
286 %! assert (mean (in, "native"), out); # logical ignores native option
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
287
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
288 ## Test single input and optional arguments "all", DIM, "omitnan")
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
289 %!test
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
290 %! x = [-10:10];
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
291 %! y = [x;x+5;x-5];
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
292 %! assert (mean (x), 0);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
293 %! assert (mean (y, 2), [0, 5, -5]');
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
294 %! assert (mean (y, "all"), 0);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
295 %! y(2,4) = NaN;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
296 %! assert (mean (y', "omitnan"), [0 5.35 -5]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
297 %! z = y + 20;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
298 %! assert (mean (z, "all"), NaN);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
299 %! m = [20 NaN 15];
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
300 %! assert (mean (z'), m);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
301 %! assert (mean (z', "includenan"), m);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
302 %! m = [20 25.35 15];
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
303 %! assert (mean (z', "omitnan"), m);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
304 %! assert (mean (z, 2, "omitnan"), m');
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
305 %! assert (mean (z, 2, "native", "omitnan"), m');
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
306 %! assert (mean (z, 2, "omitnan", "native"), m');
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
307
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
308 ## Test boolean input
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
309 %!test
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
310 %! assert (mean (true, "all"), 1);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
311 %! assert (mean (false), 0);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
312 %! assert (mean ([true false true]), 2/3, 4e-14);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
313 %! assert (mean ([true false true], 1), [1 0 1]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
314 %! assert (mean ([true false NaN], 1), [1 0 NaN]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
315 %! assert (mean ([true false NaN], 2), NaN);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
316 %! assert (mean ([true false NaN], 2, "omitnan"), 0.5);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
317 %! assert (mean ([true false NaN], 2, "omitnan", "native"), 0.5);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
318
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
319 ## Test dimension indexing with vecdim in N-dimensional arrays
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
320 %!test
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
321 %! x = repmat ([1:20;6:25], [5 2 6 3]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
322 %! assert (size (mean (x, [3 2])), [10 3]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
323 %! assert (size (mean (x, [1 2])), [6 3]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
324 %! assert (size (mean (x, [1 2 4])), [1 6]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
325 %! assert (size (mean (x, [1 4 3])), [1 40]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
326 %! assert (size (mean (x, [1 2 3 4])), [1 1]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
327
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
328 ## Test results with vecdim in N-dimensional arrays and "omitnan"
31421
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
329 %!test
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
330 %! x = repmat ([1:20;6:25], [5 2 6 3]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
331 %! m = repmat ([10.5;15.5], [5,3]);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
332 %! assert (mean (x, [3 2]), m, 4e-14);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
333 %! x(2,5,6,3) = NaN;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
334 %! m(2,3) = NaN;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
335 %! assert (mean (x, [3 2]), m, 4e-14);
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
336 %! m(2,3) = 15.52301255230125;
21962c678648 mean.m: more Matlab compatibility (bugs #58116, #50571).
Andreas Bertsatos <abertsatos@biol.uoa.gr>
parents: 30875
diff changeset
337 %! assert (mean (x, [3 2], "omitnan"), m, 4e-14);
31511
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
338
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
339 ## Test input validation
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
340 %!error <Invalid call to mean. Correct usage is> mean ()
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
341 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
342 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3, 4, 5)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
343 %!error <Invalid call to mean. Correct usage is> mean (1, "all", 3)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
344 %!error <Invalid call to mean. Correct usage is> mean (1, "b")
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
345 %!error <Invalid call to mean. Correct usage is> mean (1, 1, "foo")
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
346 %!error <X must be either a numeric or logical> mean ({1:5})
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
347 %!error <X must be either a numeric or logical> mean ("char")
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
348 %!error <DIM must be a positive integer> mean (1, ones (2,2))
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
349 %!error <DIM must be a positive integer> mean (1, 1.5)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
350 %!error <DIM must be a positive integer> mean (1, 0)
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
351 %!error <DIM must index at least N-2 dimensions of X>
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
352 %! mean (repmat ([1:20;6:25], [5 2 6 3 5]), [1 2])
9ee8943cad01 mean.m: Use Octave coding conventions.
Rik <rik@octave.org>
parents: 31421
diff changeset
353