time | Calls | line |
---|
| | 1 | function y = mean(x,dim,flag,flag2)
|
| | 2 | %MEAN Average or mean value.
|
| | 3 | % S = MEAN(X) is the mean value of the elements in X if X is a vector.
|
| | 4 | % For matrices, S is a row vector containing the mean value of each
|
| | 5 | % column.
|
| | 6 | % For N-D arrays, S is the mean value of the elements along the first
|
| | 7 | % array dimension whose size does not equal 1.
|
| | 8 | %
|
| | 9 | % MEAN(X,'all') is the mean of all elements in X.
|
| | 10 | %
|
| | 11 | % MEAN(X,DIM) takes the mean along the dimension DIM of X.
|
| | 12 | %
|
| | 13 | % MEAN(X,VECDIM) operates on the dimensions specified in the vector
|
| | 14 | % VECDIM. For example, MEAN(X,[1 2]) operates on the elements contained
|
| | 15 | % in the first and second dimensions of X.
|
| | 16 | %
|
| | 17 | % S = MEAN(...,TYPE) specifies the type in which the mean is performed,
|
| | 18 | % and the type of S. Available options are:
|
| | 19 | %
|
| | 20 | % 'double' - S has class double for any input X
|
| | 21 | % 'native' - S has the same class as X
|
| | 22 | % 'default' - If X is floating point, that is double or single,
|
| | 23 | % S has the same class as X. If X is not floating point,
|
| | 24 | % S has class double.
|
| | 25 | %
|
| | 26 | % S = MEAN(...,NANFLAG) specifies how NaN (Not-A-Number) values are
|
| | 27 | % treated. The default is 'includenan':
|
| | 28 | %
|
| | 29 | % 'includenan' - the mean of a vector containing NaN values is also NaN.
|
| | 30 | % 'omitnan' - the mean of a vector containing NaN values is the mean
|
| | 31 | % of all its non-NaN elements. If all elements are NaN,
|
| | 32 | % the result is NaN.
|
| | 33 | %
|
| | 34 | % Example:
|
| | 35 | % X = [1 2 3; 3 3 6; 4 6 8; 4 7 7]
|
| | 36 | % mean(X,1)
|
| | 37 | % mean(X,2)
|
| | 38 | %
|
| | 39 | % Class support for input X:
|
| | 40 | % float: double, single
|
| | 41 | % integer: uint8, int8, uint16, int16, uint32,
|
| | 42 | % int32, uint64, int64
|
| | 43 | %
|
| | 44 | % See also MEDIAN, STD, MIN, MAX, VAR, COV, MODE.
|
| | 45 |
|
| | 46 | % Copyright 1984-2018 The MathWorks, Inc.
|
| | 47 |
|
< 0.001 | 75 | 48 | isDimSet = nargin > 1 && ((~ischar(dim) && ~(isstring(dim) && isscalar(dim))) || ...
|
| | 49 | (~isInvalidText(dim) && strncmpi(dim,'all',max(strlength(dim), 1))));
|
< 0.001 | 75 | 50 | isFlag2Set = nargin >= 4;
|
| | 51 |
|
< 0.001 | 75 | 52 | if nargin == 1 || (nargin == 2 && isDimSet)
|
| | 53 |
|
< 0.001 | 75 | 54 | flag = 'default';
|
< 0.001 | 75 | 55 | omitnan = false;
|
| | 56 |
|
| | 57 | else % nargin >= 3 || (nargin == 2 && ~isDimSet)
|
| | 58 |
|
| | 59 | if nargin == 2
|
| | 60 | flag = dim;
|
| | 61 | elseif nargin == 3
|
| | 62 | if ~isDimSet
|
| | 63 | flag2 = dim;
|
| | 64 | isFlag2Set = true;
|
| | 65 | end
|
| | 66 | elseif nargin == 4 && ~isDimSet
|
| | 67 | error(message('MATLAB:mean:nonNumericSecondInput'));
|
| | 68 | end
|
| | 69 |
|
| | 70 | if ~isFlag2Set
|
| | 71 | flag2 = '';
|
| | 72 | end
|
| | 73 |
|
| | 74 | [flag, omitnan] = parseInputs(flag, flag2, isFlag2Set);
|
| | 75 |
|
< 0.001 | 75 | 76 | end
|
| | 77 |
|
< 0.001 | 75 | 78 | if ~isDimSet
|
| | 79 | % preserve backward compatibility with 0x0 empty
|
< 0.001 | 75 | 80 | if isequal(x,[])
|
| | 81 | y = sum(x,flag)./0;
|
| | 82 | return
|
| | 83 | end
|
< 0.001 | 75 | 84 | dim = find(size(x)~=1,1);
|
< 0.001 | 75 | 85 | if isempty(dim)
|
| | 86 | dim = 1;
|
| | 87 | end
|
| | 88 | else
|
| | 89 | if isempty(dim)
|
| | 90 | error(message('MATLAB:mean:nonNumericSecondInput'));
|
| | 91 | end
|
< 0.001 | 75 | 92 | end
|
| | 93 |
|
< 0.001 | 75 | 94 | if ~isobject(x) && isinteger(x)
|
| | 95 | % accumulation flag may still be partial
|
| | 96 | isnative = strncmpi(flag, 'native', max(1, strlength(flag)));
|
| | 97 | if intmin(class(x)) == 0 % unsigned integers
|
| | 98 | y = sum(x,dim,flag);
|
| | 99 | if (isnative && all(y(:) < intmax(class(x)))) || ...
|
| | 100 | (~isnative && all(y(:) <= flintmax))
|
| | 101 | % no precision lost, can use the sum result
|
| | 102 | y = y./mysize(x,dim);
|
| | 103 | else % throw away and recompute
|
| | 104 | y = intmean(x,dim,isnative);
|
| | 105 | end
|
| | 106 | else % signed integers
|
| | 107 | ypos = sum(max(x,0),dim,flag);
|
| | 108 | yneg = sum(min(x,0),dim,flag);
|
| | 109 | if (isnative && all(ypos(:) < intmax(class(x))) && ...
|
| | 110 | all(yneg(:) > intmin(class(x)))) || ...
|
| | 111 | (~isnative && all(ypos(:) <= flintmax) && ...
|
| | 112 | all(yneg(:) >= -flintmax))
|
| | 113 | % no precision lost, can use the sum result
|
| | 114 | y = (ypos+yneg)./mysize(x,dim);
|
| | 115 | else % throw away and recompute
|
| | 116 | y = intmean(x,dim,isnative);
|
| | 117 | end
|
| | 118 | end
|
< 0.001 | 75 | 119 | else
|
< 0.001 | 75 | 120 | if omitnan
|
| | 121 | % Compute sum and number of NaNs
|
| | 122 | m = sum(x, dim, flag, 'omitnan');
|
| | 123 | nr_nonnan = mysize(x, dim) - matlab.internal.math.countnan(x, dim);
|
| | 124 | % Divide by the number of non-NaNs.
|
| | 125 | y = m ./ nr_nonnan;
|
< 0.001 | 75 | 126 | else
|
0.003 | 75 | 127 | y = sum(x, dim, flag) ./ mysize(x,dim);
|
< 0.001 | 75 | 128 | end
|
< 0.001 | 75 | 129 | end
|
| | 130 |
|
< 0.001 | 75 | 131 | end
|
Other subfunctions in this file are not included in this listing.