Accelerating the pace of engineering and science

# Sort

Sort input elements by value

Statistics

dspstat3

## Description

The Sort block ranks the values of the input elements using either a quick sort or an insertion sort algorithm. The quick sort algorithm uses a recursive sort method and is faster at sorting more than 32 elements. The insertion sort algorithm uses a non-recursive method and is faster at sorting less than 32 elements. You should also always use the insertion sort algorithm when you are generating code from the Sort block if you do not want recursive function calls in your code. To specify the sort method, use the Sort algorithm parameter.

The Mode parameter specifies the block's mode of operation, and can be set to Value, Index, or Value and index.

### Value Mode

When Mode is set to Value, the block sorts the elements in each column of the M-by-N input matrix u in order of ascending or descending value, as specified by the Sort order parameter.

```val = sort(u)
val = flipud(sort(u))
```

The output at each sample time, val, is an M-by-N matrix containing the sorted columns of u.

The block sorts complex inputs according to their magnitude.

### Index Mode

When Mode is set to Index, the block sorts the elements in each column of the M-by-N input matrix u,

```[val,idx] = sort(u)
[val,idx] = flipud(sort(u))
```

and outputs the M-by-N index matrix, idx. The jth column of idx is an index vector that permutes the jth column of u to the desired sorting order.

```val(:,j) = u(idx(:,j),j)
```

The index value outputs are always 32-bit unsigned integer values.

### Value and Index Mode

When Mode is set to Value and index, the block outputs both the sorted matrix, val, and the index matrix, idx.

### Fixed-Point Data Types

The parameters on the Data Types pane are only used for complex fixed-point inputs. Complex fixed-point inputs are sorted by magnitude squared. The sum of the squares of the real and imaginary parts of such an input are formed before a comparison is made, as described in Value Mode. The results of the squares of the real and imaginary parts are placed into the product output data type. The result of the sum of the squares is placed into the accumulator data type. These parameters are ignored for other types of inputs.

## Dialog Box

The Main pane of the Sort block dialog appears as follows.

Mode

Specify the block's mode of operation: Output the sorted matrix (Value), the index matrix (Index), or both (Value and index).

Sort order

Specify the order in which to sort the training points, Descending or Ascending.

Sort algorithm

Specify whether the elements of the input are sorted using a Quick sort or an Insertion sort algorithm.

The Data Types pane of the Sort block dialog appears as follows.

 Note   The parameters on the Data Types pane are only used for complex fixed-point inputs. The sum of the squares of the real and imaginary parts of such an input are formed before a comparison is made, as described in Value Mode. The results of the squares of the real and imaginary parts are placed into the product output data type. The result of the sum of the squares is placed into the accumulator data type. These parameters are ignored for other types of inputs.
Rounding mode

Select the rounding mode for fixed-point operations.

Overflow mode

Select the overflow mode for fixed-point operations.

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 Show data type assistant 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 Show data type assistant button to display the Data Type Assistant, which helps you set the Accumulator data type parameter.

Lock data type settings against changes by the fixed-point tools

Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block mask.

## 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 unsigned integers

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

Val

• Double-precision floating point

• Single-precision floating point

• Fixed point (signed and unsigned)

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

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

Idx

• 32-bit unsigned integers