0
0
MATLABdata~10 mins

Matrix multiplication (*) in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Matrix multiplication (*)
Start with Matrix A and Matrix B
Check dimensions: cols of A == rows of B?
Error: Cannot multiply
For each row i in A
For each column j in B
Calculate sum of A(i,k)*B(k,j) for k
Store result in C(i,j)
Repeat for all i,j
Result: Matrix C
Matrix multiplication multiplies rows of A by columns of B, summing products to fill each element of result matrix C.
Execution Sample
MATLAB
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A * B;
Multiply two 2x2 matrices A and B to get matrix C.
Execution Table
Stepi (row in A)j (col in B)k (sum index)A(i,k)B(k,j)ProductSum so farC(i,j)
11111555
211227141919
31211666
412228162222
5211351515
621247284343
7221361818
822248325050
9-------[End: Matrix C = [19 22; 43 50]]
💡 All rows and columns processed; matrix multiplication complete.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
i-11222
j-12122
k-12122
Sum01922435050
C[0 0;0 0][19 0;0 0][19 22;0 0][19 22;43 0][19 22;43 50][19 22;43 50]
Key Moments - 3 Insights
Why do we need to check if the number of columns in A equals the number of rows in B?
Because each element in the result matrix C is computed by summing products of elements from a row in A and a column in B, so their sizes must match. See execution_table steps 1 and 5 where k runs over these matching dimensions.
Why do we sum products over k instead of just multiplying corresponding elements?
Matrix multiplication sums products over k to combine all elements in the row of A with the column of B. This is shown in execution_table where sum so far accumulates products at each k step.
Why is the result matrix C size 2x2 here?
Because A is 2x2 and B is 2x2, the result has rows from A and columns from B, so 2 rows and 2 columns. This matches the final C in execution_table step 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the sum so far for C(1,2)?
A22
B16
C6
D0
💡 Hint
Check the 'Sum so far' column at step 4 for i=1, j=2.
At which step does the element C(2,1) get its final value?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look at when sum so far reaches 43 for i=2, j=1 in execution_table.
If matrix A had 3 columns instead of 2, what would happen to the execution_table?
AMatrix multiplication would fail due to dimension mismatch
BFewer i steps would appear
CMore k steps per i,j pair would appear
DNo change in execution_table
💡 Hint
Recall k runs over columns of A and rows of B; more columns means more k iterations.
Concept Snapshot
Matrix multiplication syntax: C = A * B
- Multiply rows of A by columns of B
- Sum products over matching dimension
- Result size: rows of A x columns of B
- Requires cols of A == rows of B
- Each C(i,j) = sum_k A(i,k)*B(k,j)
Full Transcript
Matrix multiplication takes two matrices A and B. We check if the number of columns in A equals the number of rows in B. If yes, for each row i in A and each column j in B, we multiply corresponding elements A(i,k) and B(k,j) for all k, summing these products. This sum becomes the element C(i,j) in the result matrix C. The process repeats for all rows and columns, producing matrix C. The example multiplies two 2x2 matrices step-by-step, showing how each element of C is calculated by summing products. The final matrix C is [19 22; 43 50].