SME
Engineering Programming (MATLAB/Python)
Vectorized computation
Use this for “A is 3 × 4 and B is 4 × 2. What size is A × B, and how many multiplications does it take?”
Diagram
A is 3 × 4 and B is 4 × 2: the inner sizes match (4 = 4), so C = AB is 3 × 2.
Each of its 6 entries is a row of A times a column of B: 4 multiplications, 3 × 4 × 2 = 24 in all.
This is an example. Type your own values to start a new problem.
m
Rows of A
example
n
Columns of A
example
nB
Rows of B
calculated
p
Columns of B
example
rC
Rows of C
calculated
cC
Columns of C
calculated
N
Multiplications
calculated
Formulas
nB
=
n
4 = 4
rC
=
m
3 = 3
cC
=
p
2 = 2
N
=
m
×
n
×
p
24 = 3 × 4 × 2
Assumptions
•
A is m × n and B is n × p: A’s columns must equal B’s rows, or A × B is not defined.
•
Each entry of C is one row of A times one column of B: n multiplications, then their sum.
•
MATLAB’s matrix product and NumPy’s @ operator both build C this way.
Step-by-step
WE KNOW
m = 3, n = 4, p = 2
FIND
rows of B (nB), rows of C (rC), columns of C (cC), multiplications (N)
Step 1 · Find rows of B (nB)
Use nB = n
Each row of A meets a column of B entry by entry, so B needs as many rows as A has columns.
nB = 4
Step 2 · Find rows of C (rC)
Use rC = m
C gets one row for each row of A.
rC = 3
Step 3 · Find columns of C (cC)
Use cC = p
C gets one column for each column of B.
cC = 2
Step 4 · Find multiplications (N)
Use N = m × n × p
C
has
m
×
p
entries,
and
each
one
takes
n
multiplications.
N
=
3
×
4
×
2
N
=
12
×
2
N
=
24
Check
Put the numbers back in. Both sides should match.
4 = 4 ✓
3 = 3 ✓
2 = 2 ✓
24 = 3 × 4 × 2 ✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.