StatSort Method (ReadOnlyDoubleMatrix, SortDirection) |
Namespace: Novacta.Analytics
public static DoubleMatrix Sort( ReadOnlyDoubleMatrix data, SortDirection sortDirection )
Exception | Condition |
---|---|
ArgumentNullException | data is null. |
ArgumentException | sortDirection is not a field of SortDirection. |
This method returns a matrix having the same dimensions of data. The entry occupying the l-th linear position of the returned matrix is the entry of the original matrix occupying the l-th position in the requested ordering.
In the following example, a data matrix is sorted.
using System; namespace Novacta.Analytics.CodeExamples { public class SortExample0 { public void Main() { // Create a matrix. var data = new double[6] { 1, 2, 2, -3, 3, 4, }; var matrix = DoubleMatrix.Dense(3, 2, data, StorageOrder.RowMajor); Console.WriteLine("The data matrix:"); Console.WriteLine(matrix); // Sort the data in ascending order. var sortedData = Stat.Sort(matrix, SortDirection.Ascending); Console.WriteLine(); Console.WriteLine("Data sorted in ascending order:"); Console.WriteLine(sortedData); // Sort is overloaded to accept data as a read-only matrix: // sort in descending order using a read-only wrapper of the data matrix. ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly(); var sortedReadOnlyData = Stat.Sort(readOnlyMatrix, SortDirection.Descending); Console.WriteLine(); Console.WriteLine("Using read-only data. Descending sort:"); Console.WriteLine(sortedReadOnlyData); } } } // Executing method Main() produces the following output: // // The data matrix: // 1 2 // 2 -3 // 3 4 // // // // Data sorted in ascending order: // -3 2 // 1 3 // 2 4 // // // // Using read-only data. Descending sort: // 4 2 // 3 1 // 2 -3 //