Click or drag to resize
Accord.NET (logo)

MatrixSortTKey, TValue Method (TKey, TValue)

Sorts the columns of a matrix by sorting keys.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static TValue[,] Sort<TKey, TValue>(
	TKey[] keys,
	TValue[,] values
)
Request Example View Source

Parameters

keys
Type: TKey
The key value for each column.
values
Type: TValue
The matrix to be sorted.

Type Parameters

TKey
TValue

Return Value

Type: TValue
Examples
// Let's say we are in a situation where you have a NxM matrix of values and 
// a row vector 1xM where each value in the vector is associated with a column 
// in the matrix (e.g. this is the case if we had a matrix of Eigenvectors and 
// their associated eigenvalues):

double[,] matrix =
{
    { 1, 5, 3 },
    { 2, 2, 6 },
    { 1, 3, 4 },
    { 2, 4, 3 },
    { 1, 0, 1 },
};

double[] v = { -3, 1, 0.42 };

// Let's say we would like to sort the columns of the matrix 
// according to the value of the elements in the row vector:

double[,] sortedMatrix1 = Matrix.Sort(keys: v, values: matrix);

// The resulting matrix will be:
double[,] expected1 =
{
    { 1, 3, 5 },
    { 2, 6, 2 },
    { 1, 4, 3 },
    { 2, 3, 4 },
    { 1, 1, 0 },
};

// Now, let's say we would like to sort the columns of the matrix 
// according to the absolute value of the elements in the row vector:

double[,] sortedMatrix2 = Matrix.Sort(keys: v, values: matrix, 
    comparer: new GeneralComparer(ComparerDirection.Ascending, useAbsoluteValues: true));

// The resulting matrix will be:
double[,] expected2 =
{
    { 3, 5, 1 },
    { 6, 2, 2 },
    { 4, 3, 1 },
    { 3, 4, 2 },
    { 1, 0, 1 },
};
See Also