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.

a11a12a21a22a31a323 rows2 columnsshape = 3 x 2
Shape tells you how many rows and columns a matrix has.

Shape is usually written as:

m×nm \times n

where mm is the number of rows and nn 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.

MATH-C03-T03-001Exercise: Read a shape

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.

MATH-C03-T03-002Exercise: Predict the output length

If AA has shape 4 x 3 and xx has length 3, what is the length of AxAx?

Compute it first, then check your number.

HintUse the rows

The output length is the number of rows of AA.

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 AA 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.

MATH-C03-T03-003Exercise: Spot a shape mismatch

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.

MATH-C03-T03-004Exercise: Rows are not features by default

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.