median
The median value of the array elements.
| Library |
|
Syntax
Function call
-
M = median(A)— returns the median value of the array elementsA.-
If
A— vector, thenmedian(A)returns the median value of the elementsA. -
If
A— a nonempty matrix, thenmedian(A)— vector is a row containing the median value of each columnA. -
If
A— a multidimensional array, thenmedian(A)it operates on the first dimensionA, the size of which is not equal to1considering the elements as vectors. SizeMin this dimension , it becomes equal to1, while the dimensions of all other dimensions remain the same as inA.
-
-
M = median(A, vecdim)— returns the median value for the dimension specified in the vectorvecdim. For example, ifA— the matrix, thenmedian(A, [1 2])returns the median value for all elements of the matrixAsince each element of the matrix is contained in an array layer defined by the dimensions1and2.
-
M = median(_, missingflag)— also specifies how to handle missing values for any of the previous syntax options. For example,median(A, "omitnan")ignores all missing values when calculating the median. By defaultmedianincludes missing values.
Arguments
Input arguments
# A — input data
+
scalar | vector | the matrix | multidimensional array
Details
Input data specified as a scalar, vector, matrix, or multidimensional array.
| Типы данных |
|
# dim — the measurement for which the operation is performed
+
scalar
Details
The dimension that the operation is performed on, specified as a positive integer scalar. If no dimension is specified, then the first dimension of the array is used by default, the size of which is not equal to 1.
Dimension dim specifies a dimension whose length is reduced to 1. Size size(M, dim) equal to 1, while the dimensions of all other dimensions remain the same.
Consider the input matrix A size on :
# vecdim — measurement vector
+
vector
Details
A vector of dimensions defined as a vector of positive integers. Each element represents a dimension of the input array. The length of the output data in the specified operating measurements is 1, while the other dimensions remain unchanged.
Consider the input array A size 2×3×3. Then std(A,0,[1 2]) returns an array of size 1×1×3, the elements of which are the median values calculated for each layer A.
# missingflag — condition for missing a value
+
"includemissing" (by default) | "includenan" | "omitmissing" | "omitnan"
Details
The condition for missing a value, set as:
-
"includemissing"or"includenan"— when calculating the median value, it takes into account the missing values in the input dataA. If any element is missing in the working dimension, then the corresponding element in theMalso missing; -
"omitmissing"or"omitnan"— ignores missing values in the input data when calculating the median valueA. If all the elements in the working dimension are missing, then the corresponding element inMalso missing.
# W — weighing scheme
+
vector | the matrix | multidimensional array
Details
A weighting scheme defined as a vector, matrix, or multidimensional array. Elements W they must be non-negative.
If a weighing scheme is specified, then the function median returns the weighted median value, which is the value A related to cumulative 50% the weights set by W [1]. The weighted median is less affected by extreme values compared to the standard median.
If W — vector, it should have the same length as the working dimension. Otherwise W must have the same size as the input data.
| Типы данных |
|
Output arguments
# M is the median value
+
scalar | vector | the matrix | multidimensional array
Details
Median values returned as a scalar, vector, matrix, or multidimensional array.
Examples
The median value of the matrix elements
Details
Let’s create a matrix.
import EngeeDSP.Functions: median
A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
4×3 Matrix{Int64}:
0 1 1
2 3 2
1 3 2
4 2 2
Calculate the median value of each column.
M = median(A)
1×3 Matrix{Float64}:
1.5 2.5 2.0
Calculate the median value of each row.
M = median(A,2)
4×1 Matrix{Int64}:
1
2
2
2
Calculate the median value of all the elements of the matrix.
M = median(A,"all")
2.0
Literature
-
Weighted median. In Wikipedia, May 21, 2023. https://en.wikipedia.org/wiki/Weighted_median.