HyperMath

Sort

Sort

Previous topic Next topic No expanding text in this topic  

Sort

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

Sorts data and reorders indices.

Syntax

New_data, Indices = Sort(Data, Direction)

Arguments

Name

Description

 

Data

A vector or a matrix of real or complex data.

 

Direction

A string. Set to 'descend' to sort in descending order.  The default is 'ascend'.

Outputs

Name

Description

 

New_data

The vector (or matrix) of the sorted data.

 

Indices

The vector (or matrix) of the  indices.

Example 1

Sort the following vector in ascending order.

 

Syntax

 

V = [7, 3, 2, 9, 6];

R, IDX = Sort(V);

print(V)

print(R)

print(IDX)

print(V(IDX))

 

Results

 

[Matrix] 1 x 5

   7    3    2    9    6    // V

[Matrix] 1 x 5

   2    3    6    7    9    // R

[Matrix] 1 x 5

   3    2    5    1    4    // IDX

[Matrix] 1 x 5

   2    3    6    7    9    // V(IDX) equivalent to R

Example 2

Sort a matrix in descending order.

 

Syntax

 

M = [7, 3; 2, 9; 6, 8];

R, IDX = Sort(M, 'descend');

print(M)

print(R)

print(IDX)

 

Results

 

[Matrix] 3 x 2 // M

   7    3

   2    9

   6    8

[Matrix] 3 x 2 // R

   7    9

   6    8

   2    3

[Matrix] 3 x 2 // IDX

   1    2

   3    3

   2    1

Comments

Complex data is sorted by magnitude. Entries with the same magnitude are additionally sorted by phase.