0% found this document useful (0 votes)
4 views1 page

Matrix Multiplication Execution Pattern

The document describes a Java method for multiplying two 2x2 matrices, detailing the execution pattern of nested loops. It illustrates how the elements of the resultant matrix C are computed through iterative multiplications and additions. The final resultant matrix C is presented as: [[7, 10], [15, 22]].

Uploaded by

Amol Bishu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Matrix Multiplication Execution Pattern

The document describes a Java method for multiplying two 2x2 matrices, detailing the execution pattern of nested loops. It illustrates how the elements of the resultant matrix C are computed through iterative multiplications and additions. The final resultant matrix C is presented as: [[7, 10], [15, 22]].

Uploaded by

Amol Bishu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matrix Multiplication Execution pattern:

Example: Multiply Method in Java


public static void Multiply(int[][] A,int[][] B,int[][] C)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
C[i][j] = 0;
for (int k = 0; k < 2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

Execution pattern:
OUTER LOOP: i = 0 -> 0 < 2(true)
j = 0 -> 0 < 2 (true)
C[0][0] = 0
Inner loop k:
 k = 0 -> 0 < 2 (true)
C[0][0] += A[0][0] * B[0][0]
= 0 + (1 × 1) = 1
 k = 1 -> 1 < 2 (true)
C[0][0] += A[0][1] * B[1][0]
= 1 + (2 × 3) = 7
 k = 2 -> 2 < 2 (false)
Final:
C[0][0] = 7 -> j = 0 + 1

j = 1 -> 1 < 2 (true)


C[0][1] = 0
Inner loop k:
 k = 0 -> 0 < 2 (true)
C[0][1] += A[0][0] * B[0][1]
= 0 + (1 × 2) = 2
 k = 1 -> 1 < 2 (true)
C[0][1] += A[0][1] * B[1][1]
= 2 + (2 × 4) = 10
 k = 2 -> 2 < 2 (false)
Final:
C[0][1] = 10 -> j = 1 + 1

j = 2 -> 2 < 2 (false) -> i = 0 + 1


OUTER LOOP: i = 1 -> 1 < 2 (true)
j = 0
C[1][0] = 0
Inner loop k:
 k = 0 -> 0 < 2 (true)
C[1][0] += A[1][0] * B[0][0]
= 0 + (3 × 1) = 3
 k = 1 -> 1 < 2 (true)
C[1][0] += A[1][1] * B[1][0]
= 3 + (4 × 3) = 15
 k = 2 -> 2 < 2 (false)
Final:
C[1][0] = 15 -> j = 0 + 1

j = 1 -> 1 < 2 (true)


C[1][1] = 0
Inner loop k:
 k = 0 -> 0 < 2 (true)
C[1][1] += A[1][0] * B[0][1]
= 0 + (3 × 2) = 6
 k = 1 -> 1 < 2 (true)
C[1][1] += A[1][1] * B[1][1]
= 6 + (4 × 4) = 22
 k= 2 -> 2 < 2 (false)
Final:
C[1][1] = 22 -> j = 1 + 1
j = 2 -> 2 < 2 (false)
i = 1 + 1
i = 2 -> 2 < 2 (false)

FINAL RESULTANT MATRIX: After all iterations


C = 7 10
15 22

You might also like