{"record":{"id":"7b7728d34b5d9e60","repo":"TheAlgorithms/C-Sharp","slug":"dimensions-of-matrices-must-be-the-same","errorCode":null,"errorMessage":"Dimensions of matrices must be the same","messagePattern":"Dimensions of matrices must be the same","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Utilities/Extensions/MatrixExtensions.cs","lineNumber":115,"sourceCode":"            result[i] = resultMatrix[i, 0];\n        }\n\n        return result;\n    }\n\n    /// <summary>\n    ///     Performs matrix subtraction.\n    /// </summary>\n    /// <param name=\"lhs\">The LHS matrix.</param>\n    /// <param name=\"rhs\">The RHS matrix.</param>\n    /// <returns>The difference of the two matrices.</returns>\n    /// <exception cref=\"ArgumentException\">Dimensions of matrices do not match.</exception>\n    public static double[,] Subtract(this double[,] lhs, double[,] rhs)\n    {\n        if (lhs.GetLength(0) != rhs.GetLength(0) ||\n            lhs.GetLength(1) != rhs.GetLength(1))\n        {\n            throw new ArgumentException(\"Dimensions of matrices must be the same\");\n        }\n\n        var result = new double[lhs.GetLength(0), lhs.GetLength(1)];\n        for (var i = 0; i < lhs.GetLength(0); i++)\n        {\n            for (var j = 0; j < lhs.GetLength(1); j++)\n            {\n                result[i, j] = lhs[i, j] - rhs[i, j];\n            }\n        }\n\n        return result;\n    }\n\n    /// <summary>\n    ///     Performs an element by element comparison on both matrices.\n    /// </summary>\n    /// <param name=\"source\">Source left matrix.</param>","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Utilities/Extensions/MatrixExtensions.cs#L97-L133","documentation":"MatrixExtensions.Subtract performs element-wise subtraction, which requires both matrices to have identical dimensions. If either dimension differs (lhs.GetLength(0/1) != rhs.GetLength(0/1)) it throws ArgumentException because element-wise ops are undefined for mismatched shapes.","triggerScenarios":"Calling lhs.Subtract(rhs) where row counts or column counts differ, e.g. a 2x3 minus a 3x2, or a batch result minus a single row.","commonSituations":"Subtracting matrices produced from different-sized inputs earlier in a pipeline; broadcasting a 1D row against a 2D matrix expecting NumPy-like broadcasting; off-by-one in generated matrix sizes.","solutions":["Check lhs and rhs have identical GetLength(0) and GetLength(1) before subtracting","Resize or slice one matrix to match the other if the intent is element-wise on a common region","Implement explicit broadcasting logic if NumPy-style semantics are desired"],"exampleFix":"// before\nvar diff = a.Subtract(b); // a: 2x3, b: 3x3 -> throws\n// after\nif (a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1))\n{\n    throw new ArgumentException(\"Matrices must have the same dimensions for subtraction.\");\n}\nvar diff = a.Subtract(b);","handlingStrategy":"validation","validationCode":"if (lhs.GetLength(0) != rhs.GetLength(0) || lhs.GetLength(1) != rhs.GetLength(1))\n    throw new ArgumentException(\"Element-wise subtract requires identical shapes.\");\nvar diff = lhs.Subtract(rhs);","typeGuard":null,"tryCatchPattern":"try { var diff = lhs.Subtract(rhs); }\ncatch (ArgumentException) { /* reshape or report dimension mismatch upstream */ }","preventionTips":["Don't rely on broadcasting; resize matrices explicitly first","Verify producing steps output same-shaped matrices (assert in tests)","Keep matrix dimension constants centralized so pipelines stay consistent"],"tags":["c-sharp","matrix","linear-algebra","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"}