Matrix Shape and Size#
The shape of a matrix tells you its dimensions: how many rows and columns it has.
Dimensions#
A matrix with \(m\) rows and \(n\) columns is called an “\(m \times n\) matrix” (read as “m by n”).
This is a \(2 \times 3\) matrix: 2 rows, 3 columns.
Shape in Python#
Python Example
Inspect matrix dimensions
Click Run to execute the example.
Common Matrix Shapes#
Square Matrix#
A matrix with the same number of rows and columns:
This is a \(2 \times 2\) square matrix. Square matrices have special properties we’ll explore later.
Column Vector#
An \(m \times 1\) matrix:
Shape: \(3 \times 1\)
Row Vector#
A \(1 \times n\) matrix:
Shape: \(1 \times 3\)
Why Shape Matters#
The shape of a matrix determines:
What operations are valid: You can’t add a \(2 \times 3\) matrix to a \(3 \times 2\) matrix
The result of operations: Multiplying a \(2 \times 3\) matrix by a \(3 \times 2\) matrix gives a \(2 \times 2\) result
What the matrix represents: A \(1920 \times 1080\) matrix might represent an HD image
Shape Compatibility#
For matrix addition, shapes must match exactly:
Python Example
Check shape compatibility for addition
Click Run to execute the example.
For matrix multiplication, the number of columns in the first matrix must equal the number of rows in the second. We’ll cover this in detail in Matrix Multiplication.
Reshaping Matrices#
Sometimes you need to change a matrix’s shape:
Python Example
Reshape the same data
Click Run to execute the example.
The total number of elements must stay the same (here, 6 elements).
Try It#
In the reshape example, try reshaping the six values into:
1 x 66 x 12 x 34 x 2
Which ones work? Which one fails, and why?
Key Takeaways#
Matrix shape is written as rows by columns.
Addition requires matching shapes.
Matrix multiplication has a different compatibility rule.
Reshaping changes the layout, but the total number of elements must stay the same.
Next Steps#
Now that you understand matrix shapes, let’s learn about Matrix Addition, the simplest matrix operation.