0
0
R Programmingprogramming~10 mins

Matrix multiplication (%*%) in R Programming - 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
Multiply rows of A by columns of B
Sum products for each element
Build Result Matrix
End
Matrix multiplication takes two matrices, checks if they can be multiplied, then multiplies rows of the first by columns of the second, summing products to form the result.
Execution Sample
R Programming
A <- matrix(c(1,2,3,4), nrow=2)
B <- matrix(c(5,6,7,8), nrow=2)
C <- A %*% B
print(C)
Multiply two 2x2 matrices A and B using %*% and print the result.
Execution Table
StepActionMatrix AMatrix BCalculationResult Matrix C
1Define Matrix A[1 3; 2 4][ ]N/A[ ]
2Define Matrix B[1 3; 2 4][5 7; 6 8]N/A[ ]
3Check dimensions2x22x2cols A=2 == rows B=2Proceed
4Calculate C[1,1]Row1 A: (1,3)Col1 B: (5,6)1*5 + 3*6 = 5 + 18 = 23C[1,1] = 23
5Calculate C[1,2]Row1 A: (1,3)Col2 B: (7,8)1*7 + 3*8 = 7 + 24 = 31C[1,2] = 31
6Calculate C[2,1]Row2 A: (2,4)Col1 B: (5,6)2*5 + 4*6 = 10 + 24 = 34C[2,1] = 34
7Calculate C[2,2]Row2 A: (2,4)Col2 B: (7,8)2*7 + 4*8 = 14 + 32 = 46C[2,2] = 46
8Final Result[1 3; 2 4][5 7; 6 8]All elements calculated[23 31; 34 46]
💡 All elements of result matrix C calculated; multiplication complete.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 7Final
A[1 3; 2 4][1 3; 2 4][1 3; 2 4][1 3; 2 4][1 3; 2 4][1 3; 2 4]
B[ ][5 7; 6 8][5 7; 6 8][5 7; 6 8][5 7; 6 8][5 7; 6 8]
C[ ][23 NA; NA NA][23 31; NA NA][23 31; 34 NA][23 31; 34 46][23 31; 34 46]
Key Moments - 3 Insights
Why must the number of columns in Matrix A equal the number of rows in Matrix B?
Because each element in the result is a sum of products between a row of A and a column of B, their lengths must match (see Step 3 in execution_table).
How is each element of the result matrix calculated?
Each element is the sum of multiplying corresponding elements from a row of A and a column of B (see Steps 4-7 in execution_table).
Why does the result matrix have dimensions equal to rows of A and columns of B?
Because each row of A combines with each column of B to produce one element, so result size is rows of A by columns of B (see Step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4. What is the value of C[1,1]?
A34
B23
C31
D46
💡 Hint
Check the 'Result Matrix C' column at Step 4 in execution_table.
At which step does the condition for matrix multiplication dimensions get checked?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look for the step mentioning 'Check dimensions' in execution_table.
If Matrix B had 3 rows instead of 2, what would happen at Step 3?
AResult matrix would be larger
BMultiplication proceeds normally
CError because cols of A != rows of B
DNo change in calculation
💡 Hint
Refer to the dimension check in Step 3 of execution_table.
Concept Snapshot
Matrix multiplication in R uses %*% operator.
Matrices A (m x n) and B (n x p) multiply if n matches.
Result is matrix C (m x p).
Each element C[i,j] = sum of A's row i * B's column j.
Use %*% for matrix product, not * which is element-wise.
Full Transcript
Matrix multiplication in R uses the %*% operator to multiply two matrices. First, the number of columns in the first matrix must equal the number of rows in the second matrix. Then, each element of the result matrix is calculated by multiplying elements from a row of the first matrix by elements from a column of the second matrix and summing these products. The result matrix has dimensions equal to the number of rows of the first matrix and the number of columns of the second matrix. This process is shown step-by-step in the execution table, where each element of the result matrix is computed. If the dimensions do not match, multiplication cannot proceed.