{"record":{"id":"1ca51069fab38b7d","repo":"TheAlgorithms/C-Sharp","slug":"the-column-vector-should-have-only-1-element-in-width","errorCode":null,"errorMessage":"The column vector should have only 1 element in width.","messagePattern":"The column vector should have only 1 element in width\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Utilities/Extensions/VectorExtensions.cs","lineNumber":118,"sourceCode":"        for (var i = 0; i < source.Length; i++)\n        {\n            columnVector[i, 0] = source[i];\n        }\n\n        return columnVector;\n    }\n\n    /// <summary>\n    ///     Transpose column vector to 1d row vector.\n    /// </summary>\n    /// <param name=\"source\">Input column vector.</param>\n    /// <returns>Row vector.</returns>\n    /// <exception cref=\"InvalidOperationException\">The column vector should have only 1 element in width.</exception>\n    public static double[] ToRowVector(this double[,] source)\n    {\n        if (source.GetLength(1) != 1)\n        {\n            throw new InvalidOperationException(\"The column vector should have only 1 element in width.\");\n        }\n\n        var rowVector = new double[source.Length];\n\n        for (var i = 0; i < rowVector.Length; i++)\n        {\n            rowVector[i] = source[i, 0];\n        }\n\n        return rowVector;\n    }\n\n    /// <summary>\n    ///     Generates a diagonal matrix from an specified vector.\n    /// </summary>\n    /// <param name=\"vector\">The input vector.</param>\n    /// <returns>A Diagonal matrix.</returns>\n    public static double[,] ToDiagonalMatrix(this double[] vector)","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Utilities/Extensions/VectorExtensions.cs#L100-L136","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the input matrix has exactly one column (GetLength(1) == 1) before converting","If a row vector was meant, use the corresponding ToColumnVector-style path or transpose appropriately","Check upstream matrix operations so a Nx1 shape is actually produced (e.g. multiply by a 1-column operand)"],"exampleFix":"// before\nvar row = matrix.ToRowVector(); // matrix is 3x2 -> throws\n// after\nif (matrix.GetLength(1) != 1)\n{\n    throw new InvalidOperationException(\"Expected a column vector (single-column matrix).\");\n}\nvar row = matrix.ToRowVector();","handlingStrategy":"validation","validationCode":"if (source == null || source.GetLength(1) != 1)\n    throw new ArgumentException(\"ToRowVector expects a single-column (Nx1) matrix.\");\nvar row = source.ToRowVector();","typeGuard":null,"tryCatchPattern":"try { var row = source.ToRowVector(); }\ncatch (InvalidOperationException) { /* input wasn't a column vector — reshape or transpose */ }","preventionTips":["Use matrix-vector (not matrix-matrix) multiplication when a vector result is needed","Keep a naming convention (colVec/rowVec) to make 2D vector shapes visible","Transpose explicitly when converting between row and column conventions"],"tags":["c-sharp","vector","matrix","dimension-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}