Sort
Sort input elements by value
Libraries:
DSP System Toolbox /
Statistics
Description
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
.
Examples
Sort Data into Ascending and Descending Orders
This example shows how to use the Sort block to sort the input data into ascending and descending orders. The Array Plot shows the sorted and the unsorted data.
Open the SortExample
model. The input in the model is a sinusoidal signal. The data in this signal is sorted into ascending and descending orders by the two Sort blocks. The blocks use the Quick sort
algorithm to sort the data. You can also specify the blocks to output the index of the sorted values by setting the Mode parameter to Index
or Value and Index
.
Run the model. View the unsorted data and compare it with the sorted data in the Array Plot window.
Ports
Input
Port_1 — Data input
vector | matrix
The block accepts real-valued or complex-valued multichannel inputs. The input data type must be double precision, single precision, integer, or fixed point, with power-of-two slope and zero bias.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fixed point
Complex Number Support: Yes
Output
Val — Sorted data
vector | matrix
The 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.
Dependencies
To enable this port, set the Mode parameter
to Value and index
or
Value
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fixed point
Complex Number Support: Yes
Idx — Index of the sorted data
vector | matrix
The output at this port contains the indices of the sorted data.
Dependencies
To enable this port, set the Mode parameter
to Value and index
or
Index
.
Data Types: uint32
Parameters
Main Tab
Mode — Specify whether block returns values, indices, or both
Value and Index
(default) | Value
| Index
When the Mode parameter is set to:
Value
— The block sorts the elements in each channel of the M-by-N 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 M-by-N 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 M-by-N input matrix, and outputs the index array, I. Each element in I is an integer of typeuint32
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 sorting
Ascending
(default) | Descending
Specify to sort the input data in either ascending or descending order.
Sort algorithm — Sort method
Quick 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.
Data Types Tab
Note
To use these parameters, the data input must be complex and fixed point. For all other inputs, the parameters on the Data Types tab are ignored. 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.
Rounding mode — Method of rounding operation
Floor
(default) | Ceiling
| Convergent
| Nearest
| Round
| Simplest
| Zero
Specify the rounding mode for fixed-point operations. For more details, see rounding mode.
Saturate on integer overflow — Method of overflow action
off (default) | on
When you select this parameter, the block saturates the result of its
fixed-point operation. When you clear this parameter, the block wraps
the result of its fixed-point operation. For details on
saturate
and wrap
, see overflow
mode for fixed-point operations.
Product output — Product output data type
Inherit: 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, binary-point, scaled, fixed-point 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 type
Inherit: 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, binary-point, scaled, fixed-point 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 fixed-point tools — Prevent fixed-point tools from overriding data types
off (default) | on
Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block.
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Algorithms
Sort
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')
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Generated code relies on the memcpy
or
memset
function (string.h
) under certain
conditions.
Version History
Introduced before R2006a
MATLAB Command
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.
Select a Web Site
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: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)