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