/// <summary> /// Sorts the given Matrix by the specified row or column selector and returns the new Matrix /// along with the original indices. /// </summary> /// <param name="source">The Matrix</param> /// <param name="keySelector">Property selector to sort by.</param> /// <param name="t">Specifies whether to sort horizontally or vertically.</param> /// <param name="ascending">Determines whether to sort ascending or descending (Default: True)</param> /// <param name="indices">Vector of <paramref name="t"/> indices in the original Matrix before the sort operation.</param> /// <returns>New Matrix and Vector of original indices.</returns> 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)); }
/// <summary> /// Unshapes the given Matrix into a Vector form along the <paramref name="dimensionType"/> axis. /// <para>Reads from the source Matrix and stacks from right to left when <paramref name="dimensionType"/> equals 'Col' otherwise uses a bottom up approach.</para> /// </summary> /// <param name="m">The Matrix to act on.</param> /// <param name="dimensionType">Type of the dimension to use when unrolling the Matrix.</param> /// <returns>Matrix.</returns> public static Vector Unshape(Matrix m, VectorType dimensionType = VectorType.Col) { return(Vector.Combine((dimensionType == VectorType.Col ? m.GetCols().ToArray() : m.GetRows().ToArray()))); }
/// <summary> /// Returns a vector of the median values for each row or column. /// </summary> /// <param name="source">Matrix.</param> /// <param name="t">VectorType.</param> /// <returns></returns> public static Vector Median(Matrix source, VectorType t = VectorType.Col) { var vectors = (t == VectorType.Row ? source.GetCols() : source.GetRows()); return(vectors.Select(s => s.Median()).ToVector()); }
/// <summary> /// Slices the input matrix using starting and stopping positions. /// </summary> /// <param name="m">Source matrix.</param> /// <param name="minIndex">Minimum index to slice from.</param> /// <param name="maxIndex">Maximum index to slice.</param> /// <param name="t"></param> /// <returns></returns> public static Matrix Slice(this Matrix m, int minIndex, int maxIndex, VectorType t = VectorType.Row) { IEnumerable <Vector> array = (t == VectorType.Row ? m.GetRows() : m.GetCols()); return(array.Skip(minIndex).Take((maxIndex - minIndex) + 1).ToMatrix(t)); }
/// <summary> /// Slices the input matrix using starting and stopping positions. /// </summary> /// <param name="m">Source matrix.</param> /// <param name="minIndex">Minimum index to slice from.</param> /// <param name="maxIndex">Maximum index to slice.</param> /// <param name="t"></param> /// <returns></returns> public static Matrix Slice(this Matrix m, int minIndex, int maxIndex, VectorType t = VectorType.Row) { var array = t == VectorType.Row ? m.GetRows() : m.GetCols(); return(array.Skip(minIndex).Take(maxIndex - minIndex + 1).ToMatrix(t)); }
/// <summary> /// Unshapes the given Matrix into a Vector form along the <paramref name="dimensionType"/> axis. /// <para>Reads from the source Matrix and stacks from right to left when <paramref name="dimensionType"/> equals 'Col' otherwise uses a bottom up approach.</para> /// </summary> /// <param name="m">The Matrix to act on.</param> /// <param name="dimensionType">Type of the dimension to use when unrolling the Matrix.</param> /// <returns>Matrix.</returns> public static Vector Unshape(Matrix m, VectorType dimensionType = VectorType.Col) { return Vector.Combine((dimensionType == VectorType.Col ? m.GetCols().ToArray() : m.GetRows().ToArray())); }
/// <summary> /// Sorts the given Matrix by the specified row or column selector and returns the new Matrix /// along with the original indices. /// </summary> /// <param name="source">The Matrix</param> /// <param name="keySelector">Property selector to sort by.</param> /// <param name="t">Specifies whether to sort horizontally or vertically.</param> /// <param name="ascending">Determines whether to sort ascending or descending (Default: True)</param> /// <param name="indices">Vector of <paramref name="t"/> indices in the original Matrix before the sort operation.</param> /// <returns>New Matrix and Vector of original indices.</returns> 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); }
/// <summary> /// Returns a vector of the median values for each row or column. /// </summary> /// <param name="source">Matrix.</param> /// <param name="t">VectorType.</param> /// <returns></returns> public static Vector Median(Matrix source, VectorType t = VectorType.Col) { var vectors = (t == VectorType.Row ? source.GetCols() : source.GetRows()); return vectors.Select(s => s.Median()).ToVector(); }