Col 3 Col 2 Col 1 Row 1

  • Slides: 18
Download presentation

Col 3 Col 2 Col 1 Row 1 X[1][1] X[1][2] X[1][3] Row 2 X[2][1]

Col 3 Col 2 Col 1 Row 1 X[1][1] X[1][2] X[1][3] Row 2 X[2][1] X[2][2] X[2][3] Row 3 X[3][1] X[3][2] X[3][3] Here x is a 3 x 3 matrix i. e. r = 3 and c = 3 So, how can we rewrite the above matrix in ARRAYS’ format of C program?

Col 1 Col 2 Col 3 1 2 0 X[0][0] X[0][1] X[0][2] 1 X[1][0]

Col 1 Col 2 Col 3 1 2 0 X[0][0] X[0][1] X[0][2] 1 X[1][0] X[1][1] X[1][2] 2 X[2][0] X[2][1] x[3][3] X[2][2] i= 0 j= Row 1 Row 2 Row 3

Now we assume a matrix m[3][3] = 6 4 7 5 2 9 8

Now we assume a matrix m[3][3] = 6 4 7 5 2 9 8 3 1

Col 1 Col 2 Col 3 0 1 2 0 6 5 8 1

Col 1 Col 2 Col 3 0 1 2 0 6 5 8 1 4 2 3 2 7 9 1 j= i= Row 1 Row 2 Row 3 Row and column wise distribution of the values of matrix m[3][3]

How can we read the values of matrix m[3][3]?

How can we read the values of matrix m[3][3]?

for(i = 0; i < 3; i = i + 1) { for(j =

for(i = 0; i < 3; i = i + 1) { for(j = 0; j < 3; j = j + 1) { scanf(“%d”, &m[i][j]); } } So, what happened then……. .

Row 2 Row 3 Col 3 Row 1 Col 2 i= Col 1 j=

Row 2 Row 3 Col 3 Row 1 Col 2 i= Col 1 j= 0 1 2 0 m[0][0] = 6 m[0][1] = 5 m[0][2] = 8 1 m[1][0] = 4 m[1][1] = 2 m[1][2] = 3 2 m[2][0] = 7 m[2][1] = 9 m[2][2] = 1 m[3][3]

Again for the matrix n[3][3] = 12 20 14 38 15 29 35 23

Again for the matrix n[3][3] = 12 20 14 38 15 29 35 23 17

So, we read the values of matrix n[3][3] to follow the above process

So, we read the values of matrix n[3][3] to follow the above process

for(i = 0; i < 3; i = i + 1) { for(j =

for(i = 0; i < 3; i = i + 1) { for(j = 0; j < 3; j = j + 1) { scanf(“%d”, &n[i][j]); } } So, what next……. .

0 Col 3 i= Col 2 Col 1 j= 1 2 Row 1 0

0 Col 3 i= Col 2 Col 1 j= 1 2 Row 1 0 n[0][0] = 12 n[0][1] = 38 n[0][2] = 35 Row 2 1 n[1][0] = 20 n[1][1] = 15 n[1][2] = 23 Row 3 2 n[2][0] = 14 n[2][1] = 29 n[2][2] = 17 Matrix element’s with their position in the specific cells

Now, how can you add these matrices m[3][3] and n[3][3] m[3][3] = 6 5

Now, how can you add these matrices m[3][3] and n[3][3] m[3][3] = 6 5 8 4 2 7 9 ? n[3][3] = 12 38 35 3 20 15 23 1 14 29 17

0 0 1 2 m[0][0] + n[0][0] m[0][1] + n[0][1] m[0][2] + n[0][2] =

0 0 1 2 m[0][0] + n[0][0] m[0][1] + n[0][1] m[0][2] + n[0][2] = 6 = 18 + 12 = 5 = 43 + 38 = 8 + = 43 35 1 m[1][0] + n[1][0] = 4 + 20 = 24 m[1][1] + n[1][1] = 2 + 15 = 17 m[1][2] + n[1][2] = 3 + 23 = 26 2 m[2][0] + n[2][0] = 7 + 14 = 21 m[2][1] + n[2][1] = 9 + 29 = 38 m[2][2] + n[2][2] = 1 + 17 = 18 addmn[3][3]

for(i = 0; i < 3; i = i + 1) { for(j =

for(i = 0; i < 3; i = i + 1) { for(j = 0; j < 3; j = j + 1) { addmn[i][j] = m[i][j] + n[i][j]; } } Then you will get the addition matrix………

But, if you want to multiply these matrices m[3][3] and n[3][3], what’s the scenario

But, if you want to multiply these matrices m[3][3] and n[3][3], what’s the scenario will be…. . ? m[3][3] = 6 5 8 4 2 7 9 n[3][3] = 12 38 35 3 20 15 23 1 14 29 17

0 0 1 m[0][0] * n[0][0] + m[0][1] * n[1][0] + m[0][2] * n[2][0]

0 0 1 m[0][0] * n[0][0] + m[0][1] * n[1][0] + m[0][2] * n[2][0] = 6*12 + 5*20 + 8*14 = 72 + 100 + 112 = 284 1 2 promn[3][3] 2

//calculation part of matrix multiplication for (i=0 ; i < 3; i++) { for

//calculation part of matrix multiplication for (i=0 ; i < 3; i++) { for (j=0 ; j < 3; j++) { for (k=0 ; k < 3; k++) { sum = sum + a[i] [k] * b[k] [j] } c[i] [j] = sum; sum = 0; } }