TheAlgorithms/C-Sharp · error · InvalidOperationException

The column vector should have only 1 element in width.

Error message

The column vector should have only 1 element in width.

What it means

VectorExtensions.ToRowVector converts a 2D matrix that represents a column vector into a 1D row vector; this only makes sense when the matrix is a single column. If source.GetLength(1) != 1 it throws InvalidOperationException because the input is not a column vector.

Solutions

  1. Ensure the input matrix has exactly one column (GetLength(1) == 1) before converting
  2. If a row vector was meant, use the corresponding ToColumnVector-style path or transpose appropriately
  3. Check upstream matrix operations so a Nx1 shape is actually produced (e.g. multiply by a 1-column operand)

Example fix

// before
var row = matrix.ToRowVector(); // matrix is 3x2 -> throws
// after
if (matrix.GetLength(1) != 1)
{
    throw new InvalidOperationException("Expected a column vector (single-column matrix).");
}
var row = matrix.ToRowVector();
Defensive patterns

Strategy: validation

Validate before calling

if (source == null || source.GetLength(1) != 1)
    throw new ArgumentException("ToRowVector expects a single-column (Nx1) matrix.");
var row = source.ToRowVector();

Try / catch

try { var row = source.ToRowVector(); }
catch (InvalidOperationException) { /* input wasn't a column vector — reshape or transpose */ }

Prevention

When it happens

Trigger: Calling ToRowVector on a matrix with more than one column, e.g. a 3x2 matrix, or on a full matrix result mistaken for a vector.

Common situations: Passing the output of a matrix-matrix multiply (which can widen) where a matrix-vector multiply was intended; confusing row/column vector conventions after porting MATLAB/Python code; forgetting that the result of Multiply is 2D regardless of shape.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/1ca51069fab38b7d. Report an issue: GitHub.

Appendix: source

Thrown at Utilities/Extensions/VectorExtensions.cs:118

        for (var i = 0; i < source.Length; i++)
        {
            columnVector[i, 0] = source[i];
        }

        return columnVector;
    }

    /// <summary>
    ///     Transpose column vector to 1d row vector.
    /// </summary>
    /// <param name="source">Input column vector.</param>
    /// <returns>Row vector.</returns>
    /// <exception cref="InvalidOperationException">The column vector should have only 1 element in width.</exception>
    public static double[] ToRowVector(this double[,] source)
    {
        if (source.GetLength(1) != 1)
        {
            throw new InvalidOperationException("The column vector should have only 1 element in width.");
        }

        var rowVector = new double[source.Length];

        for (var i = 0; i < rowVector.Length; i++)
        {
            rowVector[i] = source[i, 0];
        }

        return rowVector;
    }

    /// <summary>
    ///     Generates a diagonal matrix from an specified vector.
    /// </summary>
    /// <param name="vector">The input vector.</param>
    /// <returns>A Diagonal matrix.</returns>
    public static double[,] ToDiagonalMatrix(this double[] vector)

View on GitHub (pinned to 96e2905cab)