{"record":{"id":"455ca6bd0e949c76","repo":"3b1b/manim","slug":"index-index-out-of-bound-for-matrix-with-len-se-455ca6","errorCode":null,"errorMessage":"Index {index} out of bound for matrix with {len(self.rows)} rows","messagePattern":"Index (.+?) out of bound for matrix with (.+?) rows","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"manimlib/mobject/matrix.py","lineNumber":142,"sourceCode":"            R\"\\left[\\begin{array}{c}\",\n            *len(rows) * [R\"\\quad \\\\\"],\n            R\"\\end{array}\\right]\",\n        )))\n        brackets.set_height(rows.get_height() + v_buff)\n        l_bracket = brackets[:len(brackets) // 2]\n        r_bracket = brackets[len(brackets) // 2:]\n        l_bracket.next_to(rows, LEFT, h_buff)\n        r_bracket.next_to(rows, RIGHT, h_buff)\n        return VGroup(l_bracket, r_bracket)\n\n    def get_column(self, index: int):\n        if not 0 <= index < len(self.columns):\n            raise IndexError(f\"Index {index} out of bound for matrix with {len(self.columns)} columns\")\n        return self.columns[index]\n\n    def get_row(self, index: int):\n        if not 0 <= index < len(self.rows):\n            raise IndexError(f\"Index {index} out of bound for matrix with {len(self.rows)} rows\")\n        return self.rows[index]\n\n    def get_columns(self) -> VGroup:\n        return self.columns\n\n    def get_rows(self) -> VGroup:\n        return self.rows\n\n    def set_column_colors(self, *colors: ManimColor) -> Self:\n        columns = self.get_columns()\n        for color, column in zip(colors, columns):\n            column.set_color(color)\n        return self\n\n    def add_background_to_entries(self) -> Self:\n        for mob in self.get_entries():\n            mob.add_background_rectangle()\n        return self","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/3b1b/manim/blob/dee01804d47b9f94402d71472674710dcac125b8/manimlib/mobject/matrix.py#L124-L160","documentation":"IndexError from Matrix.get_row (manimlib/mobject/matrix.py:142) with message 'Index {index} out of bound for matrix with {n} rows'. self.rows holds the row VGroups from construction; get_row enforces 0 <= index < len(self.rows), rejecting both too-large and negative indices before indexing.","triggerScenarios":"Matrix([[1,2],[3,4]]).get_row(2); get_row(-1); row indices derived from matrix height that were computed for a different dataset; iterating rows of a transposed expectation (columns count vs rows count mix-up).","commonSituations":"Labeling/highlighting a specific row in teaching animations with a hardcoded index after shrinking the data; loops with range(len(matrix)) where matrix length equals rows but rows vs columns got swapped; 1-based indexing assumptions.","solutions":["Use 0-based row indices strictly less than len(matrix.get_rows())","Compute bounds from the object: n_rows = len(m.get_rows()); highlight = m.get_row(n_rows - 1)","Double-check whether you want get_row or get_column when data shape changed"],"exampleFix":"# before\nm = Matrix([[1, 2], [3, 4]])\nm.get_row(5).set_color(RED)  # raises\n\n# after\nm = Matrix([[1, 2], [3, 4]])\nif 0 <= 5 < len(m.get_rows()):\n    m.get_row(5).set_color(RED)\nelse:\n    m.get_row(-0 + len(m.get_rows()) - 1).set_color(RED)","handlingStrategy":"validation","validationCode":"n_rows = len(matrix.get_rows())\nassert 0 <= row_index < n_rows, f\"row_index must be in [0, {n_rows})\"\nrow = matrix.get_row(row_index)","typeGuard":"def is_valid_row_index(matrix, i: int) -> bool:\n    return 0 <= i < len(matrix.get_rows())","tryCatchPattern":null,"preventionTips":["Use 0-based indices derived from len(matrix.get_rows())","Negative indices are rejected; use n_rows - 1 for the last row","Verify rows vs columns when data shape changes"],"tags":["manim","matrix","index-error","validation"],"backgroundTag":null,"analyzedSha":"dee01804d47b9f94402d71472674710dcac125b8","analyzedAt":"2026-08-14T19:53:44.241Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}