Matrix: Thinking in 2D
A matrix is a powerful way to store data in a two-dimensional format. While a standard list is like a single row of lockers, a matrix is like an entire wall of lockers with both rows and columns. This structure is essential for complex tasks like Machine Learning or processing digital images.
How Computers "See" Images
The image file example is the best way to visualize this. A computer doesn't "see" a photo of a dog or a sunset; it only understands numbers.
- The Grid: Every digital image is actually a grid of tiny squares called pixels.
- The Values: Each pixel is assigned a number representing its color or brightness.
- The Matrix: To keep these pixels in the right order so the image looks correct, the computer stores them in a matrix.
If you have a black-and-white photo, the matrix might be filled with 0s (for black) and 1s (for white). When the computer reads that matrix, it knows exactly where to place each dot of light on your screen to recreate the image.
Nested List
In Python, we create a matrix by placing lists inside of a larger list. This is often called a Nested List.
Think of the outer list as the "Cabinet" and each inner list as a "Shelf."
The Basic Matrix Structure
To create a 3x3 matrix (3 rows and 3 columns), we write it like this:
matrix = [
[1, 2, 3], # Row 0
[4, 5, 6], # Row 1
[7, 8, 9] # Row 2
]
The Image Example: Binary Art
If we use your example of a computer reading an image, a simple "X" shape in a 3x3 black-and-white grid would look like this in code:
image_matrix = [
[1, 0, 1], # White, Black, White
[0, 1, 0], # Black, White, Black
[1, 0, 1] # White, Black, White
]
How to Access Data
To grab a specific "pixel," you use two sets of square brackets: matrix[row][column].
To get the center number (5) from the first example: matrix[1][1]
To get the top-right number (3): matrix[0][2]
Example:
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
Try splitting the matrix:
Test Your Knowledge
Select the correct answer below to see if you've mastered Python math logic.
Loading quiz...
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named matrix.py in your part21 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: The Treasure Map:
In the matrix below, a "Treasure" (the number 100) is hidden. Write the exact line of code using matrix[row][column] that would print the treasure.
matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 100] ]
Exercise 2: Fixing the Error:
You have a 2x2 matrix representing a scoreboard. Currently, the score is tied at 0. Write a line of code to change Team B's score (the second 0 in the second row) to 7.
scoreboard = [
[0, 0], # Team A
[0, 0] # Team B
]
Exercise 3: The Identity Matrix:
An "Identity Matrix" is a square matrix where there are 1s on the diagonal (from top-left to bottom-right) and 0s everywhere else. Complete the middle row of this 3x3 matrix to make it a perfect Identity Matrix.
identity = [
[1, 0, 0],
[?, ?, ?],
[0, 0, 1]
]
Don't Forget to commit and Push!