Rows, Columns, and Shapes
The shape of a matrix is:
number of rows x number of columns
A matrix with 3 rows and 2 columns has shape 3 x 2.
Shape is usually written as:
where is the number of rows and is the number of columns.
This order is not decorative. Rows describe how many output entries a matrix-vector product will produce. Columns describe how many input entries each row expects to read. Shape is a contract between the data you have and the operation you are trying to run.
A matrix has 5 rows and 2 columns. What is its first shape number?
Compute it first, then check your number.
HintRows first
In m x n, m is the number of rows.
SolutionFirst shape number
The shape is 5 x 2, so the first shape number is 5. Matrix shapes are
written rows first, then columns.
Why Shape Matters
Shape tells us what operations are allowed.
If a matrix has shape 3 x 2, each row has two entries. That means it can
multiply a vector with two entries and produce a vector with three entries.
The input length must match the number of columns.
A Small Rule
If:
A has shape m x n
x has length n
then:
Ax has length m
The inner number n must match. The output uses the outer number m.
If has shape 4 x 3 and has length 3, what is the length of ?
Compute it first, then check your number.
HintUse the rows
The output length is the number of rows of .
SolutionOuter shape
The inner dimension 3 matches the vector length. The output length is the
number of rows: 4.
When Shapes Do Not Fit
If has shape 4 x 3, it cannot multiply a length-2 vector.
The matrix rows each have three entries, so each row expects to take a dot product with a vector that also has three entries.
Enter 1 if a 4 x 3 matrix can multiply a length-2 vector, or 0 if it
cannot.
Compute it first, then check your number.
HintCheck columns
Compare the matrix column count with the vector length.
SolutionInner dimension mismatch
Enter 0. A 4 x 3 matrix has 3 columns, so it expects a length-3
vector. A length-2 vector does not fit.
A matrix has shape 100 x 12. In a dataset table, this often means 100
examples and 12 features. If you multiply this matrix by a length-12
weight vector, how many outputs do you get?
Compute it first, then check your number.
HintOne row, one dot product
The vector length matches the number of columns. Each row produces one output.
SolutionOutput per row
The matrix has 100 rows. Since the weight vector has length 12, each
row can take one dot product with it. The result has 100 outputs.
Next, we compute the operation that shape was preparing us for.