time | calls | line |
---|
| | 1 | function y = mean(x,dim,flag)
|
| | 2 | %MEAN Average or mean value.
|
| | 3 | % S = MEAN(X) is the mean value of the elements in X
|
| | 4 | % if X is a vector. For matrices, S is a row
|
| | 5 | % vector containing the mean value of each column.
|
| | 6 | % For N-D arrays, S is the mean value of the
|
| | 7 | % elements along the first array dimension whose size
|
| | 8 | % does not equal 1.
|
| | 9 | % If X is floating point, that is double or single,
|
| | 10 | % S has the same class as X. If X is of integer
|
| | 11 | % class, S has class double.
|
| | 12 | %
|
| | 13 | % MEAN(X,DIM) takes the mean along the dimension DIM
|
| | 14 | % of X.
|
| | 15 | %
|
| | 16 | % S = MEAN(X,'double') and S = MEAN(X,DIM,'double')
|
| | 17 | % returns S in double, even if X is single.
|
| | 18 | %
|
| | 19 | % S = MEAN(X,'native') and S = MEAN(X,DIM,'native')
|
| | 20 | % returns S in the same class as X.
|
| | 21 | %
|
| | 22 | % S = MEAN(X,'default') and S = MEAN(X,DIM,'default')
|
| | 23 | % are equivalent to S = MEAN(X) and S = MEAN(X,DIM)
|
| | 24 | % respectively.
|
| | 25 | %
|
| | 26 | % Example: If X = [1 2 3; 3 3 6; 4 6 8; 4 7 7];
|
| | 27 | %
|
| | 28 | % then mean(X,1) is [3.0000 4.5000 6.0000] and
|
| | 29 | % mean(X,2) is [2.0000 4.0000 6.0000 6.0000].'
|
| | 30 | %
|
| | 31 | % Class support for input X:
|
| | 32 | % float: double, single
|
| | 33 | % integer: uint8, int8, uint16, int16, uint32,
|
| | 34 | % int32, uint64, int64
|
| | 35 | %
|
| | 36 | % See also MEDIAN, STD, MIN, MAX, VAR, COV, MODE.
|
| | 37 |
|
| | 38 | % Copyright 1984-2013 The MathWorks, Inc.
|
| | 39 |
|
< 0.01 | 841 | 40 | if nargin==2 && ischar(dim)
|
| | 41 | flag = dim;
|
| 841 | 42 | elseif nargin < 3
|
| 841 | 43 | flag = 'default';
|
| 841 | 44 | end
|
| | 45 |
|
| 841 | 46 | if nargin == 1 || (nargin == 2 && ischar(dim))
|
| | 47 | % preserve backward compatibility with 0x0 empty
|
< 0.01 | 840 | 48 | if isequal(x,[])
|
| | 49 | y = sum(x,flag)/0;
|
| | 50 | return
|
| | 51 | end
|
| | 52 |
|
0.02 | 840 | 53 | dim = find(size(x)~=1,1);
|
| 840 | 54 | if isempty(dim), dim = 1; end
|
| 840 | 55 | end
|
| | 56 |
|
| 841 | 57 | if ~isobject(x) && isinteger(x)
|
| | 58 | isnative = strcmp(flag,'native');
|
| | 59 | if intmin(class(x)) == 0 % unsigned integers
|
| | 60 | y = sum(x,dim,flag);
|
| | 61 | if (isnative && all(y(:) < intmax(class(x)))) || ...
|
| | 62 | (~isnative && all(y(:) <= flintmax))
|
| | 63 | % no precision lost, can use the sum result
|
| | 64 | y = y/size(x,dim);
|
| | 65 | else % throw away and recompute
|
| | 66 | y = intmean(x,dim,flag);
|
| | 67 | end
|
| | 68 | else % signed integers
|
| | 69 | ypos = sum(max(x,0),dim,flag);
|
| | 70 | yneg = sum(min(x,0),dim,flag);
|
| | 71 | if (isnative && all(ypos(:) < intmax(class(x))) && ...
|
| | 72 | all(yneg(:) > intmin(class(x)))) || ...
|
| | 73 | (~isnative && all(ypos(:) <= flintmax) && ...
|
| | 74 | all(yneg(:) >= -flintmax))
|
| | 75 | % no precision lost, can use the sum result
|
| | 76 | y = (ypos+yneg)/size(x,dim);
|
| | 77 | else % throw away and recompute
|
| | 78 | y = intmean(x,dim,flag);
|
| | 79 | end
|
| | 80 | end
|
| 841 | 81 | else
|
0.14 | 841 | 82 | y = sum(x,dim,flag)/size(x,dim);
|
| 841 | 83 | end
|
| | 84 |
|
< 0.01 | 841 | 85 | end
|