numl.Math.LinearAlgebra.Matrix.Sort C# (CSharp) Method

Sort() public static method

Sorts the given Matrix by the specified row or column selector and returns the new Matrix along with the original indices.
public static Sort ( Matrix source, double>.Func keySelector, VectorType t, bool ascending, Vector &indices ) : Matrix
source Matrix The Matrix
keySelector double>.Func Property selector to sort by.
t VectorType Specifies whether to sort horizontally or vertically.
ascending bool Determines whether to sort ascending or descending (Default: True)
indices Vector Vector of indices in the original Matrix before the sort operation.
return Matrix
        public static Matrix Sort(Matrix source, Func<Vector, double> keySelector, VectorType t, bool ascending, out Vector indices)
        {
            int max = (t == VectorType.Row ? source.Rows : source.Cols);
            indices = Vector.Zeros(max);

            List<Vector> vects = new List<Vector>(max);

            IEnumerable<Vector> arrays = (t == VectorType.Row ? source.GetRows() : source.GetCols());

            KeyValuePair<Vector, int>[] sort = (ascending ?
                                                      arrays.Select((i, v) => new KeyValuePair<Vector, int>(i, v))
                                                            .OrderBy(o => keySelector(o.Key))
                                                         :
                                                      arrays.Select((i, v) => new KeyValuePair<Vector, int>(i, v))
                                                            .OrderByDescending(o => keySelector(o.Key))).ToArray();

            indices = sort.Select(s => s.Value).ToVector();

            return sort.Select(s => s.Key).ToMatrix(t);
        }

Same methods

Matrix::Sort ( Matrix source, double>.Func keySelector, VectorType t, bool ascending = true ) : Matrix

Usage Example

示例#1
0
 /// <summary>
 ///   Sorts the given Matrix by the specified row or column index and returns the new Matrix
 ///   along with the original indices.
 /// </summary>
 /// <param name="m">The Matrix</param>
 /// <param name="keySelector">Property selector to sort by.</param>
 /// <param name="t">
 ///   Specifies whether to sort horizontally (<see cref="VectorType.Col" />) or vertically (
 ///   <see cref="VectorType.Row" />).
 /// </param>
 /// <param name="isAscending">Determines whether to sort ascending or descending (Default: True)</param>
 /// <param name="indices">Vector of the original (<paramref name="t" />) indices before the sort operation.</param>
 /// <returns>New Matrix and Vector of original indices.</returns>
 public static Matrix Sort(
     this Matrix m,
     Func <Vector, double> keySelector,
     VectorType t,
     bool isAscending,
     out Vector indices)
 {
     return(Matrix.Sort(m, keySelector, t, isAscending, out indices));
 }
All Usage Examples Of numl.Math.LinearAlgebra.Matrix::Sort