Sort input elements by value
DSP System Toolbox / Statistics
The Sort block ranks the values of the input elements along each
channel (column) in an Ascending
or a
Descending
order, based on the Sort
order you specify. Complex inputs are sorted by their magnitude, which is
the sum of the squares of the real and imaginary components of the input. You can choose
the Sort algorithm to be either Quick
sort
or Insertion sort
. The quick sort
algorithm uses a recursive sort method and is faster at sorting more than 32 elements.
The insertion sort algorithm uses a nonrecursive method and is faster at sorting fewer
than 32 elements. When you generate code, use the insertion sort algorithm to avoid
recursive function calls.
The Mode parameter specifies the block's mode of operation, which
you can set to Value
, Index
, or
Value and Index
.
Port_1
— Data inputThe block accepts realvalued or complexvalued multichannel inputs. The input data type must be double precision, single precision, integer, or fixed point, with poweroftwo slope and zero bias.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Complex Number Support: Yes
Val
— Sorted dataThe block sorts the data along each channel and outputs the sorted data through this port. The size, data type, and complexity of the sorted data matches that of the input data. The block sorts complex inputs according to their magnitude.
To enable this port, set the Mode parameter
to Value and index
or
Value
.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Complex Number Support: Yes
Idx
— Index of the sorted dataThe output at this port contains the indices of the sorted data.
To enable this port, set the Mode parameter
to Value and index
or
Index
.
Data Types: uint32
Mode
— Specify whether block returns values, indices, or bothValue and Index
(default)  Value
 Index
When the Mode parameter is set to:
Value
— The block sorts
the elements in each channel of the
MbyN input
matrix in an ascending or descending order, based on what
you specify in the Sort order
parameter. The output at each sample time,
Val, is an
MbyN matrix that
contains the sorted columns of the input.
The block sorts complex inputs according to their magnitude.
Index
— The block sorts
the elements in each channel of the
MbyN input
matrix, and outputs the index array, I.
Each element in I is an integer of type
uint32
that indexes the sorted value
in the corresponding column of the input.
Value and index
— The
block outputs the sorted values of the input data,
Val, and the corresponding indices in
the index array, I.
Sort order
— Order of sortingAscending
(default)  Descending
Specify to sort the input data in either ascending or descending order.
Sort algorithm
— Sort methodQuick sort
(default)  Insertion sort
The quick sort algorithm uses a recursive sort method and is faster at sorting more than 32 elements. The insertion sort algorithm uses a nonrecursive method and is faster at sorting fewer than 32 elements. When you generate code, to avoid recursive function calls, use the insertion sort algorithm.
To use these parameters, the data input must be complex and fixedpoint. For all other inputs, the parameters on the Data Types tab are ignored.
Rounding mode
— Method of rounding operationFloor
(default)  Ceiling
 Convergent
 Nearest
 Round
 Simplest
 Zero
Specify the rounding mode for fixedpoint operations. For more details, see rounding mode.
Saturate on integer overflow
— Method of overflow actionWhen you select this parameter, the block saturates the result of its
fixedpoint operation. When you clear this parameter, the block wraps
the result of its fixedpoint operation. For details on
saturate
and wrap
, see overflow
mode for fixedpoint operations.
Product output
— Product output data typeInherit: Same as
input
(default)  fixdt([],16,0)
The squares of the real and imaginary parts of the complex input are stored in the Product output data type.
You can set this parameter to:
Inherit: Same as input
— The
product output data type is the same as the input data
type.
fixdt([],16,0)
— The product
output data type is an autosigned, binarypoint, scaled,
fixedpoint data type with a word length of 16 bits and a
fraction length of 0.
Alternatively, you can set the Product output data type by using the Data Type Assistant. To use the assistant, click the Show data type assistant button.
For more information on the data type assistant, see Specify Data Types Using Data Type Assistant (Simulink).
Accumulator
— Accumulator data typeInherit: Same as product
output
(default)  Inherit: Same as input
 fixdt([],16,0)
The result of the sum of the squares of the real and imaginary parts of the complex input are stored in the Accumulator data type.
You can set this parameter to:
Inherit: Same as product output
— The accumulator data type is the same as the product
output data type.
Inherit: Same as input
— The
accumulator data type is the same as the input data type.
fixdt([],16,0)
— The
accumulator data type is an autosigned, binarypoint, scaled,
fixedpoint data type with a word length of 16 bits and a
fraction length of 0.
Alternatively, you can set the Accumulator data type by using the Data Type Assistant. To use the assistant, click the Show data type assistant button.
For more information on the data type assistant, see Specify Data Types Using Data Type Assistant (Simulink).
Lock data type settings against changes by the fixedpoint tools
— Prevent fixedpoint tools from overriding data typesSelect this parameter to prevent the fixedpoint tools from overriding the data types you specify on the block.
Data Types 

Direct Feedthrough 

Multidimensional Signals 

VariableSize Signals 

ZeroCrossing Detection 

The block produces results identical to the MATLAB^{®}
sort
function.
The output of the block is equivalent to the following MATLAB code, when Sort order is set to:
Ascending
— [Val,I] =
sort(u,'ascend')
Descending
— [Val,I] =
sort(u,'descend')
where:
u
is the data input.
Val
is the sorted output.
I
is the index of the sorted output.
When the input is complex, the block sorts the data according to the magnitude.
The block computes the magnitude by taking the sum of the squares of the real and
imaginary components of the complex input. This is identical to calling the
sort
function as [Val,I] =
sort(u,...,'ComparisonMethod','abs')
.
Generated code relies on memcpy
or memset
functions (string.h) under certain conditions.
The parameters on the Data Types tab are used only for complex fixedpoint inputs. Complex inputs are sorted by their magnitude, which is the sum of the squares of the real and imaginary components of the input. The results of the squares of the real and imaginary parts are stored in the Product output data type. The result of the sum of the squares is stored in the Accumulator data type. The parameters on the Data Types tab are ignored for all other inputs.
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.