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

Reshape() public static method

Reshapes the supplied Vector into a Matrix form.
public static Reshape ( Vector v, int rows, int columns, VectorType byVector = VectorType.Row ) : Matrix
v Vector Vector to reshape.
rows int Height of the matrix to return
columns int Width of the matrix to return
byVector VectorType Direction to process, i.e. Row = Fill Down then Right, or Col = Fill Right then Down
return Matrix
        public static Matrix Reshape(Vector v, int rows, int columns, VectorType byVector = VectorType.Row)
        {
            if (rows * columns != v.Length)
                throw new InvalidOperationException(
                    string.Format("Cannot reshape Vector of length {0} into a {1} x {2} Matrix.", v.Length, rows, columns));

            Matrix m = new Matrix(rows, columns);

            int counter = 0;

            switch (byVector)
            {
                case VectorType.Row:
                    {
                        for (int i = 0; i < columns; i++)
                            for (int k = 0; k < rows; k++)
                                m[k, i] = v[counter++];
                    } break;
                case VectorType.Col:
                    {
                        for (int i = 0; i < rows; i++)
                            for (int k = 0; k < columns; k++)
                                m[i, k] = v[counter++];
                    } break;
            }

            return m;
        }

Same methods

Matrix::Reshape ( Matrix m, int rows, int cols ) : Matrix
Matrix::Reshape ( Vector v, int dimension, VectorType dimensionType = VectorType.Col, VectorType byVector = VectorType.Row ) : Matrix

Usage Example

Exemplo n.º 1
0
 /// <summary>
 /// Reshapes the given Vector into a Matrix form given the specified dimension.
 /// <para>Reads from the source vector and repopulates from left to right when <paramref name="vectorType"/> equals 'Col' otherwise uses top down approach.</para>
 /// </summary>
 /// <param name="source"></param>
 /// <param name="dimension">If the <paramref name="vectorType"/> equals 'Col' the <paramref name="dimension"/> becomes the column width otherwise it is row height.</param>
 /// <param name="vectorType">Unit type of the dimension to use when rebuilding the Matrix.</param>
 /// <param name="fillType">Direction to process, i.e. Row = Fill Down then Right, or Col = Fill Right then Down</param>
 /// <returns>Matrix.</returns>
 public static Matrix Reshape(this Vector source, int dimension, VectorType vectorType = VectorType.Col, VectorType fillType = VectorType.Row)
 {
     return(Matrix.Reshape(source, dimension, vectorType, fillType));
 }