time | calls | line |
---|
| | 1 | function y = median(x,dim,flag)
|
| | 2 | %MEDIAN Median value.
|
| | 3 | % For vectors, MEDIAN(x) is the median value of the elements in x.
|
| | 4 | % For matrices, MEDIAN(X) is a row vector containing the median value
|
| | 5 | % of each column. For N-D arrays, MEDIAN(X) is the median value of the
|
| | 6 | % elements along the first non-singleton dimension of X.
|
| | 7 | %
|
| | 8 | % MEDIAN(X, DIM) takes the median along the dimension DIM of X.
|
| | 9 | %
|
| | 10 | % MEDIAN(..., MISSING) specifies how NaN (Not-A-Number) values
|
| | 11 | % are treated. The default is 'includenan':
|
| | 12 | %
|
| | 13 | % 'includenan' - the median of a vector containing NaN values is also NaN.
|
| | 14 | % 'omitnan' - the median of a vector containing NaN values is the
|
| | 15 | % median of all its non-NaN elements. If all elements
|
| | 16 | % are NaN, the result is NaN.
|
| | 17 | %
|
| | 18 | % Example: If X = [1 2 4 4; 3 4 6 6; 5 6 8 8; 5 6 8 8];
|
| | 19 | %
|
| | 20 | % then median(X) is [4 5 7 7] and median(X,2) is [3; 5; 7; 7]
|
| | 21 | %
|
| | 22 | % Class support for input X:
|
| | 23 | % float: double, single
|
| | 24 | % integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
|
| | 25 | %
|
| | 26 | % See also MEAN, STD, MIN, MAX, VAR, COV, MODE.
|
| | 27 |
|
| | 28 | % Copyright 1984-2014 The MathWorks, Inc.
|
| | 29 |
|
| 1 | 30 | if isempty(x) % Not checking flag in this case
|
| | 31 | if nargin == 1 || (nargin == 2 && ischar(dim))
|
| | 32 |
|
| | 33 | % The output size for [] is a special case when DIM is not given.
|
| | 34 | if isequal(x,[])
|
| | 35 | if isinteger(x)
|
| | 36 | y = zeros('like',x);
|
| | 37 | else
|
| | 38 | y = nan('like',x);
|
| | 39 | end
|
| | 40 | return;
|
| | 41 | end
|
| | 42 |
|
| | 43 | % Determine first nonsingleton dimension
|
| | 44 | dim = find(size(x)~=1,1);
|
| | 45 |
|
| | 46 | end
|
| | 47 |
|
| | 48 | s = size(x);
|
| | 49 | if dim <= length(s)
|
| | 50 | s(dim) = 1; % Set size to 1 along dimension
|
| | 51 | end
|
| | 52 | if isinteger(x)
|
| | 53 | y = zeros(s,'like',x);
|
| | 54 | else
|
| | 55 | y = nan(s,'like',x);
|
| | 56 | end
|
| | 57 |
|
| | 58 | return;
|
| | 59 | end
|
| | 60 |
|
| 1 | 61 | omitnan = false;
|
| 1 | 62 | dimSet = true;
|
| 1 | 63 | if nargin == 1
|
| 1 | 64 | dimSet = false;
|
| | 65 | elseif nargin == 2 && ischar(dim)
|
| | 66 | flag = dim;
|
| | 67 | dimSet = false;
|
| | 68 | end
|
| | 69 |
|
| 1 | 70 | if nargin == 2 && dimSet == false || nargin == 3
|
| | 71 | len = max(length(flag), 1);
|
| | 72 |
|
| | 73 | if ~isrow(flag)
|
| | 74 | error(message('MATLAB:median:unknownFlag'));
|
| | 75 | end
|
| | 76 |
|
| | 77 | s = strncmpi(flag, {'omitnan', 'includenan'}, len);
|
| | 78 |
|
| | 79 | if ~any(s)
|
| | 80 | error(message('MATLAB:median:unknownFlag'));
|
| | 81 | end
|
| | 82 |
|
| | 83 | omitnan = s(1);
|
| | 84 | end
|
| | 85 |
|
| 1 | 86 | sz = size(x);
|
| | 87 |
|
| 1 | 88 | if dimSet
|
| | 89 | if ~isscalar(dim) || ~isreal(dim) || floor(dim) ~= ceil(dim) || dim < 1
|
| | 90 | error(message('MATLAB:getdimarg:dimensionMustBePositiveInteger'));
|
| | 91 | end
|
| | 92 |
|
| | 93 | if dim > numel(sz)
|
| | 94 | y = x;
|
| | 95 | return;
|
| | 96 | end
|
| | 97 | end
|
| | 98 |
|
| | 99 |
|
| 1 | 100 | if isvector(x) && (~dimSet || sz(dim) > 1)
|
| | 101 | % If input is a vector, calculate single value of output.
|
0.01 | 1 | 102 | x = sort(x);
|
| 1 | 103 | nCompare = length(x);
|
| 1 | 104 | if isnan(x(nCompare)) % Check last index for NaN
|
| | 105 | if omitnan
|
| | 106 | nCompare = find(~isnan(x), 1, 'last');
|
| | 107 | if isempty(nCompare)
|
| | 108 | y = nan('like',x);
|
| | 109 | return;
|
| | 110 | end
|
| | 111 | else
|
| | 112 | y = nan('like',x);
|
| | 113 | return;
|
| | 114 | end
|
| | 115 | end
|
| 1 | 116 | half = floor(nCompare/2);
|
| 1 | 117 | y = x(half+1);
|
| 1 | 118 | if 2*half == nCompare % Average if even number of elements
|
| 1 | 119 | y = meanof(x(half),y);
|
| 1 | 120 | end
|
| | 121 | else
|
| | 122 | if ~dimSet % Determine first nonsingleton dimension
|
| | 123 | dim = find(sz ~= 1,1);
|
| | 124 | end
|
| | 125 |
|
| | 126 | % Reshape and permute x into a matrix of size sz(dim) x (numel(x) / sz(dim))
|
| | 127 | if dim ~= 1
|
| | 128 | n1 = prod(sz(1:dim-1));
|
| | 129 | n2 = prod(sz(dim+1:end));
|
| | 130 | if n2 == 1 % because reshape(x, [n1, n2, 1]) errors for sparse matrices
|
| | 131 | x = reshape(x, [n1, sz(dim)]);
|
| | 132 | else
|
| | 133 | x = reshape(x, [n1, sz(dim), n2]);
|
| | 134 | end
|
| | 135 | x = permute(x, [2 1 3]);
|
| | 136 | end
|
| | 137 | x = x(:, :);
|
| | 138 |
|
| | 139 | % Sort along columns
|
| | 140 | x = sort(x, 1);
|
| | 141 |
|
| | 142 | if ~omitnan || all(~isnan(x(end, :)))
|
| | 143 | % Use vectorized method with column
|
| | 144 | % indexing. Reshape at end to appropriate dimension.
|
| | 145 | nCompare = sz(dim); % Number of elements used to generate a median
|
| | 146 | half = floor(nCompare/2); % Midway point, used for median calculation
|
| | 147 |
|
| | 148 | y = x(half+1,:);
|
| | 149 | if 2*half == nCompare
|
| | 150 | y = meanof(x(half,:),y);
|
| | 151 | end
|
| | 152 |
|
| | 153 | y(isnan(x(nCompare,:))) = NaN; % Check last index for NaN
|
| | 154 |
|
| | 155 | else
|
| | 156 |
|
| | 157 | % Get median of the non-NaN values in each column.
|
| | 158 | y = nan(1, size(x, 2), 'like', x);
|
| | 159 |
|
| | 160 | % Number of non-NaN values in each column
|
| | 161 | n = sum(~isnan(x), 1);
|
| | 162 |
|
| | 163 | % Deal with all columns that have an odd number of valid values
|
| | 164 | oddCols = find((n>0) & rem(n,2)==1);
|
| | 165 | oddIdxs = sub2ind(size(x), (n(oddCols)+1)/2, oddCols);
|
| | 166 | y(oddCols) = x(oddIdxs);
|
| | 167 |
|
| | 168 | % Deal with all columns that have an even number of valid values
|
| | 169 | evenCols = find((n>0) & rem(n,2)==0);
|
| | 170 | evenIdxs = sub2ind(size(x), n(evenCols)/2, evenCols);
|
| | 171 | y(evenCols) = meanof( x(evenIdxs), x(evenIdxs+1) );
|
| | 172 |
|
| | 173 | end
|
| | 174 |
|
| | 175 | % Now reshape output.
|
| | 176 | sz(dim) = 1;
|
| | 177 | y = reshape(y, sz);
|
| | 178 | end
|
Other subfunctions in this file are not included in this listing.