Lec13 PDF
Lec13 PDF
Lec13 PDF
1 −25 10
29 1 −18
C
2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)
sum = 0;
1 −25 10 for(k=0;k<columnsA;k++) 1 −25 10
29 1 −18
sum += A[0][k]*B[k][0];
29 1 −18
C[0][0] = sum;
C C
Example (Multiply Two Matrices) Example (Multiply Two Matrices)
A B A Col 0 ACol 1 B BCol 0 Col 1 Col 2
2 −1 3 −9 2 Row 0 22 −1
-1 3
Row 0−9 3 2 -9 2
× Row 1 33 44
× Row 1 5
3 4 5 7 −6 5 7 −6 7 -6
2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)
1 −25 10 for(j=0;j<columnsB;j++)
1 −25 10
29 1 −18
{
29 1 −18
sum = 0;
for(k=0;k<columnsA;k++)
C sum += A[0][k]*B[k][j];
C[0][j] = sum;
C
}
2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)
for(i=0;i<rowsA;i++)
1 −25 10 {
for(j=0;j<columnsB;j++)
1 −25 10
29 1 −18 { 29 1 −18
sum = 0;
for(k=0;k<columnsA;k++)
C sum += A[i][k]*B[k][j];
C
C[i][j] = sum;
}
}
Example (Multiply Two Matrices) 1/3 Example (Multiply Two Matrices) 2/3
int main() for(i=0;i<rowsA;i++)
{ {
int A[100][100], B[100][100], C[100][100]; for(j=0;j<columnsA;j++)
int i, j, k, rowsA, columnsA, rowsB, columnsB;
int sum; {
printf("A[%d][%d]: ",i, j);
printf("Number of rows in A: "); scanf("%d",&A[i][j]);
scanf("%d",&rowsA); }
printf("Number of columns in A: "); }
scanf("%d",&columnsA); printf("\n");
printf("Number of rows in B: ");
scanf("%d",&rowsB); for(i=0;i<rowsB;i++)
printf("Number of columns in B: "); {
scanf("%d",&columnsB); for(j=0;j<columnsB;j++)
if(columnsA != rowsB)
{
{ printf("B[%d][%d]: ",i, j);
printf("Invalid matrix dimensions\n"); scanf("%d",&B[i][j]);
return 0; }
} }