Block 14: Indexing & Slicing (2D)
Select rows, columns, and submatrices from 2D arrays.
Concepts
- arr[row, col] for single element
- arr[row, :] for full row; arr[:, col] for full column
- Sub-matrix slicing: arr[r1:r2, c1:c2]
- Difference between arr[0] and arr[0:1]
Code Examples
See exercise below.
Exercise
From a 5×5 matrix (1–25), extract: row 2, column 3, top-left 3×3 block. Replace the entire diagonal of a 4×4 matrix with zeros.
Homework
Create a checkerboard 4×4 0/1 pattern without explicit loops. Wednesday