# 2-D Median

Find 2-D Median value of each input matrix

## Library

Statistics

`visionstatistics`

• ## Description

The 2-D Median block computes the median value of each row or column of the input, along vectors of a specified dimension of the input, or of the entire input. The median of a set of input values is calculated as follows:

1. The values are sorted.

2. If the number of values is odd, the median is the middle value.

3. If the number of values is even, the median is the average of the two middle values.

For a given input `u`, the size of the output array `y` depends on the setting of the Find the median value over parameter. For example, consider a 3-dimensional input signal of size M-by-N-by-P:

• `Entire input` — The output at each sample time is a scalar that contains the median value of the M-by-N-by-P input matrix.

`y = median(u(:)) % Equivalent MATLAB code `
• `Each row` — The output at each sample time consists of an M-by-1-by-P array, where each element contains the median value of each vector over the second dimension of the input. For an input that is an M-by-N matrix, the output is an M-by-1 column vector.

`y = median(u,2) % Equivalent MATLAB code `
• `Each column` — The output at each sample time consists of a 1-by-N-by-P array, where each element contains the median value of each vector over the first dimension of the input. For an input that is an M-by-N matrix, the output at each sample time is a 1-by-N row vector.

`y = median(u) % Equivalent MATLAB code `

For convenience, length-M 1-D vector inputs are treated as M-by-1 column vectors when the block is in this mode. Sample-based length-M row vector inputs are also treated as M-by-1 column vectors.

• `Specified dimension` — The output at each sample time depends on Dimension. If Dimension is set to `1`, the output is the same as when you select `Each column`. If Dimension is set to `2`, the output is the same as when you select `Each row`. If Dimension is set to `3`, the output at each sample time is an M-by-N matrix containing the median value of each vector over the third dimension of the input.

`y = median(u,Dimension) % Equivalent MATLAB code `

The block sorts complex inputs according to their magnitude.

### Fixed-Point Data Types

For fixed-point inputs, you can specify accumulator, product output, and output data types as discussed in Parameters. Not all these fixed-point parameters are applicable for all types of fixed-point inputs. The following table shows when each kind of data type and scaling is used.

Output data typeAccumulator data typeProduct output data type

Even M

X

X

Odd M

X

Odd M and complex

X

X

X

Even M and complex

X

X

X

The accumulator and output data types and scalings are used for fixed-point signals when M is even. The result of the sum performed while calculating the average of the two central rows of the input matrix is stored in the accumulator data type and scaling. The total result of the average is then put into the output data type and scaling.

The accumulator and product output parameters are used for complex fixed-point inputs. The sum of the squares of the real and imaginary parts of such an input are formed before the input elements are sorted, as described in Description. The results of the squares of the real and imaginary parts are placed into the product output data type and scaling. The result of the sum of the squares is placed into the accumulator data type and scaling.

For fixed-point inputs that are both complex and have even M, the data types are used in all of the ways described. Therefore, in such cases, the accumulator type is used in two different ways.

## Parameters

Sort algorithm

Specify whether to sort the elements of the input using a ```Quick sort``` or an `Insertion sort` algorithm.

Find the median value over

Specify whether to find the median value along rows, columns, entire input, or the dimension specified in the Dimension parameter. For more information, see Description.

Dimension

Specify the dimension (one-based value) of the input signal, over which the median is computed. The value of this parameter cannot exceed the number of dimensions in the input signal. This parameter is only visible when the Find the median value over parameter is set to `Specified dimension`.

### Note

Floating-point inheritance takes precedence over the data type settings defined on this pane. When inputs are floating point, the block ignores these settings, and all internal data types are floating point.

Rounding mode

Select the Rounding mode for fixed-point operations.

Saturate on integer overflow

Select the overflow mode for fixed-point operations. See Precision and Range.

Product output data type

Specify the product output data type. See Fixed-Point Data Types and Multiplication Data Types for illustrations depicting the use of the product output data type in this block. You can set it to:

• A rule that inherits a data type, for example, ```Inherit: Same as input```

• An expression that evaluates to a valid data type, for example, `fixdt([],16,0)`

Click the button to display the Data Type Assistant, which helps you set the Product output data type parameter.

Accumulator data type

Specify the accumulator data type. See Fixed-Point Data Types for illustrations depicting the use of the accumulator data type in this block. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Same as product output```

• An expression that evaluates to a valid data type, for example, `fixdt([],16,0)`

Click the button to display the Data Type Assistant, which helps you set the Accumulator data type parameter.

Output data type

Specify the output data type. See Fixed-Point Data Types for illustrations depicting the use of the output data type in this block. You can set it to:

• A rule that inherits a data type, for example, ```Inherit: Same as accumulator```

• An expression that evaluates to a valid data type, for example, `fixdt([],16,0)`

Click the button to display the Data Type Assistant, which helps you set the Output data type parameter.

Minimum

Specify the minimum value that the block should output. The default value, `[]`, is equivalent to `-Inf`. Simulink® software uses this value to perform:

• Simulation range checking (see Specify Signal Ranges (Simulink))

• Automatic scaling of fixed-point data types

Maximum

Specify the maximum value that the block should output. The default value, `[]`, is equivalent to `Inf`. Simulink software uses this value to perform:

• Simulation range checking (see Specify Signal Ranges (Simulink))

• Automatic scaling of fixed-point data types

## Supported Data Types

PortSupported Data Types

Input

• Double-precision floating point

• Single-precision floating point

• Fixed point (signed and unsigned)

• 8-, 16-, 32-, and 128-bit signed integers

• 8-, 16-, 32-, and 128-bit unsigned integers

Output

• Double-precision floating point

• Single-precision floating point

• Fixed point (signed and unsigned)

• 8-, 16-, 32-, and 128-bit signed integers

• 8-, 16-, 32-, and 128-bit unsigned integers

## Examples

### Calculate Median Value Over Entire Input The ex_vision_2dmedian calculates the median value over the entire input.

 2-D Maximum Computer Vision Toolbox™ 2-D Mean Computer Vision Toolbox 2-D Minimum Computer Vision Toolbox 2-D Standard Deviation Computer Vision Toolbox 2-D Variance Computer Vision Toolbox `median` MATLAB