{"record":{"id":"0f0c8d7d8076b07e","repo":"TheAlgorithms/JavaScript","slug":"input-is-not-a-valid-2d-matrix-0f0c8d","errorCode":null,"errorMessage":"Input is not a valid 2D matrix.","messagePattern":"Input is not a valid 2D matrix\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/RowEchelon.js","lineNumber":112,"sourceCode":"\n// Subtract one row from another row\nconst subtractRow = (currentRow, fromRow, matrix) => {\n  let numCols = matrix[0].length\n  for (let j = 0; j < numCols; j++) {\n    matrix[fromRow][j] -= matrix[currentRow][j]\n  }\n}\n\n// Check if two numbers are equal within a given tolerance\nconst isTolerant = (a, b, tolerance) => {\n  const absoluteDifference = Math.abs(a - b)\n  return absoluteDifference <= tolerance\n}\n\nconst rowEchelon = (matrix) => {\n  // Check if the input matrix is valid; if not, throw an error.\n  if (!isMatrixValid(matrix)) {\n    throw new Error('Input is not a valid 2D matrix.')\n  }\n\n  let numRows = matrix.length\n  let numCols = matrix[0].length\n  let result = matrix\n\n  // Iterate through the rows (i) and columns (j) of the matrix.\n  for (let i = 0, j = 0; i < numRows && j < numCols; ) {\n    // If the current column has all zero elements below the current row,\n    // move to the next column.\n    if (!checkNonZero(i, j, result)) {\n      j++\n      continue\n    }\n\n    // Select a pivot element and normalize the current row.\n    selectPivot(i, j, result)\n    let factor = 1 / result[i][j]","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/RowEchelon.js#L94-L130","documentation":"The rowEchelon function computes the row echelon form of a matrix. The guard delegates to isMatrixValid() which checks structural integrity (non-empty, rectangular shape). If the matrix is ragged, empty, or structurally invalid, the error is thrown before any row operations begin.","triggerScenarios":"Calling rowEchelon([]), rowEchelon([[1,2],[3]]), rowEchelon([[]]), or rowEchelon(null). Ragged arrays (rows of differing lengths) and empty matrices trigger this error.","commonSituations":"Ragged matrices from inconsistent data parsing, empty arrays from filtered datasets, or matrices with rows of different lengths from CSV/JSON ingestion.","solutions":["Ensure the matrix is a non-empty rectangular 2D array.","Pad or trim ragged rows to equal length before calling.","Validate matrix structure with an isMatrixValid-equivalent pre-check."],"exampleFix":"// before\nrowEchelon([[1, 2], [3]])\n// after\nrowEchelon([[1, 2], [3, 0]])","handlingStrategy":"validation","validationCode":"const isValidMatrix = (m) =>\n  Array.isArray(m) &&\n  m.length > 0 &&\n  m.every((row) => Array.isArray(row) && row.length === m[0].length)\nif (!isValidMatrix(matrix)) {\n  throw new TypeError('Input must be a non-empty rectangular 2D matrix')\n}\nrowEchelon(matrix)","typeGuard":"const isRectangularMatrix = (m) =>\n  Array.isArray(m) &&\n  m.length > 0 &&\n  m.every((r) => Array.isArray(r) && r.length === m[0].length)","tryCatchPattern":null,"preventionTips":["Normalize matrices to rectangular shape before performing row operations.","Validate all entries are numeric before calling.","Pad or trim ragged rows to equal length during data ingestion."],"tags":["math","linear-algebra","input-validation","matrix"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}