Mercurial > octave
annotate scripts/statistics/mean.m @ 24869:170e8625562a
mean.m: add out_type option to control data type of return value
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Tue, 13 Mar 2018 14:42:29 +0100 |
parents | fdc9ce839afd |
children | d85b2485af9e |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 1995-2017 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 6 ## 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
|
7 ## 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
|
8 ## (at your option) any later version. |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
3426 | 14 ## |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## 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
|
17 ## <https://www.gnu.org/licenses/>. |
3200 | 18 |
3367 | 19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
20 ## @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
|
21 ## @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
|
22 ## @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
|
23 ## @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
|
24 ## @deftypefnx {} {} mean (@dots{}, @var{outtype}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
25 ## 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
|
26 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## 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
|
28 ## |
3367 | 29 ## @tex |
30 ## $$ {\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
|
31 ## 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
|
32 ## |
3367 | 33 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @ifnottex |
3426 | 35 ## |
3367 | 36 ## @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
|
37 ## mean (@var{x}) = SUM_i @var{x}(i) / N |
3367 | 38 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
39 ## |
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 @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
|
41 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
42 ## @end ifnottex |
3367 | 43 ## If @var{x} is a matrix, compute the mean for each column and return them |
44 ## in a row vector. | |
3200 | 45 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
46 ## 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
|
47 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
48 ## 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
|
49 ## The following options are recognized: |
3367 | 50 ## |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 ## @table @asis |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
52 ## @item @qcode{"a"} |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
53 ## Compute the (ordinary) arithmetic mean. [default] |
3367 | 54 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
55 ## @item @qcode{"g"} |
7001 | 56 ## Compute the geometric mean. |
3367 | 57 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
58 ## @item @qcode{"h"} |
3367 | 59 ## Compute the harmonic mean. |
60 ## @end table | |
4022 | 61 ## |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
62 ## 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
|
63 ## 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
|
64 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
65 ## @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
|
66 ## @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
|
67 ## 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
|
68 ## 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
|
69 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
70 ## @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
|
71 ## 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
|
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 ## @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
|
74 ## 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
|
75 ## 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
|
76 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
77 ## @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
|
78 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
79 ## 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
|
80 ## may appear first. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12454
diff
changeset
|
81 ## @seealso{median, mode} |
3367 | 82 ## @end deftypefn |
3426 | 83 |
5428 | 84 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 85 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200 | 86 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
87 function y = mean (x, varargin) |
4022 | 88 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
89 if (nargin < 1 || nargin > 4) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
90 print_usage (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
91 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
92 |
12435
0f21f258aa17
mean: also accept logical values
John W. Eaton <jwe@octave.org>
parents:
11595
diff
changeset
|
93 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
94 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
|
95 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
|
96 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
|
97 sz = size (x); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
98 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
99 ## 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
|
100 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
101 ## 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
|
102 ## MEAN_TYPE exist, they must be the first two options |
3200 | 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 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 varargin(end) = []; |
4022 | 110 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
|
111 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
112 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
117 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
|
118 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
|
119 if (! all (dim_mask | mean_type_mask)) |
6046 | 120 print_usage (); |
3200 | 121 endif |
3426 | 122 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
123 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
|
124 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
|
125 (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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
131 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
132 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
|
133 endswitch |
3200 | 134 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
141 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
|
142 endswitch |
3426 | 143 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
144 ## 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 else |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
153 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
|
154 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
155 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
|
156 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
|
157 otherwise |
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: 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
|
159 endswitch |
3426 | 160 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
161 ## 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
|
162 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
|
163 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
|
164 ## 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 ## 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
|
170 else |
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 = 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
|
172 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
173 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
174 ## 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
|
175 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
|
176 endswitch |
3200 | 177 endfunction |
7411 | 178 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
179 |
7411 | 180 %!test |
181 %! x = -10:10; | |
182 %! y = x'; | |
183 %! 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
|
184 %! assert (mean (x), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
185 %! assert (mean (y), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
186 %! assert (mean (z), [0, 10]); |
7411 | 187 |
15464
930117c97760
mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14868
diff
changeset
|
188 ## 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
|
189 %!assert (mean (repmat (0.1,1,1000), "g"), 0.1, 20*eps) |
7411 | 190 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 %!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
|
192 %!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
|
193 %!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
|
194 %!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
|
195 %!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
|
196 %!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
|
197 %!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
|
198 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
199 ## 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
|
200 %!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
|
201 %!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
|
202 %!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
|
203 %!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
|
204 %!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
|
205 %!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
|
206 %!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
|
207 %!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
|
208 %!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
|
209 %!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
|
210 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
211 ## 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
|
212 %!test |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
213 %! 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
|
214 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
215 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
216 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
217 %! |
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 = 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
|
219 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
220 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
221 %! assert (mean (in, "default"), single (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
222 %! assert (mean (in, "double"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
223 %! assert (mean (in, "native"), single (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
224 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
225 %! 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
|
226 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
227 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
228 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
229 %! assert (mean (in, "double"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
230 %! assert (mean (in, "native"), uint8 (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
231 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
232 %! 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
|
233 %! out = 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
|
234 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
235 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
236 %! assert (mean (in, "native"), out) # logical ignores native option |