Engee documentation

median

The median value of the array elements.

Library

EngeeDSP

Syntax

Function call

  • M = median(A) — returns the median value of the array elements A.

    • If A — vector, then median(A) returns the median value of the elements A.

    • If A — a nonempty matrix, then median(A) — vector is a row containing the median value of each column A.

    • If A — a multidimensional array, then median(A) it operates on the first dimension A, the size of which is not equal to 1 considering the elements as vectors. Size M in this dimension , it becomes equal to 1, while the dimensions of all other dimensions remain the same as in A.

  • M = median(A, "all") — returns the median value for all elements A.

  • M = median(A, dim) — returns the median value by dimension dim. For example, if A — the matrix, then median(A, 2) returns a column vector containing the median value of each row.

  • M = median(A, vecdim) — returns the median value for the dimension specified in the vector vecdim. For example, if A — the matrix, then median(A, [1 2]) returns the median value for all elements of the matrix A since each element of the matrix is contained in an array layer defined by the dimensions 1 and 2.

  • 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 default median includes missing values.

  • M = median(_, Weight=W) — defines the weighing scheme W and returns the weighted median value.

Arguments

Input arguments

# A — input data

+ scalar | vector | the matrix | multidimensional array

Details

Input data specified as a scalar, vector, matrix, or multidimensional array.

Типы данных

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

# 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 :

  • median(A, 1) calculates the median value of the elements in each column of the matrix A and returns a vector string of the size 1 on .

median 1

  • median(A, 2) calculates the median value of the elements in each row of the matrix A and returns a column vector of size 1 on .

median 2

# 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.

std 3 en

# 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 data A. If any element is missing in the working dimension, then the corresponding element in the M also missing;

  • "omitmissing" or "omitnan" — ignores missing values in the input data when calculating the median value A. If all the elements in the working dimension are missing, then the corresponding element in M also 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.

You cannot specify this argument if an argument is used. vecdim or a method M = median(A, "all").

Типы данных

Float32, Float64

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

  1. Weighted median. In Wikipedia, May 21, 2023. https://en.wikipedia.org/wiki/Weighted_median.