Engineering Programming (MATLAB/Python) · Problem type

Broadcasting shapes

Use this for “NumPy adds an array of shape (3, 1) to one of shape (1, 4). What shape is the result?”

Diagram

A (3, 1)+B (1, 4)= result (3, 4), 12 entries
Rows: the 1 stretches to 3; columns: the 1 stretches to 4.
The result is (3, 4): E = 3 × 4 = 12 entries.
This is an example. Type your own values to start a new problem.
a₁
Rows of A
example
a₂
Columns of A
example
b₁
Rows of B
example
b₂
Columns of B
example
r₁
Rows of the result
calculated
r₂
Columns of the result
calculated
E
Entries in the result
calculated

Formulas

r₁
=
max(
a₁
,
b₁
)
3 = max(3, 1)
r₂
=
max(
a₂
,
b₂
)
4 = max(1, 4)
E
=
r₁
×
r₂
12 = 3 × 4

Assumptions

•
NumPy compares the shapes axis by axis: the sizes must be equal, or one of them must be 1.
•
A size of 1 is repeated to match the other, so the result takes the larger size.
•
MATLAB since R2016b expands element-wise operators the same way (implicit expansion).

Step-by-step

WE KNOW
a₁ = 3, a₂ = 1, b₁ = 1, b₂ = 4
FIND
rows of the result (r₁), columns of the result (r₂), entries in the result (E)
Step 1 · Find rows of the result (r₁)
Use r₁ = max(a₁, b₁)
Equal rows stay as they are; a 1 is repeated to match the other, so the result takes the larger.
r₁
=
max(3,
1)
r₁
=
3
Step 2 · Find columns of the result (r₂)
Use r₂ = max(a₂, b₂)
Equal columns stay as they are; a 1 is repeated to match the other, so the result takes the larger.
r₂
=
max(1,
4)
r₂
=
4
Step 3 · Find entries in the result (E)
Use E = r₁ × r₂
The
result
has
r₁
rows
of
r₂
entries
each.
E
=
3
×
4
E
=
12
Check
Put the numbers back in. Both sides should match.
3 = max(3, 1) ✓
4 = max(1, 4) ✓
12 = 3 × 4 ✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.

Related lessons

© 2026 SME. All rights reserved.