为什么我们在矩阵乘法程序中使用c [i] [j] = c [i] [j] +(a [i] [k] * b [k] [j])。 [英] why we use c[i][j] = c[i][j] + (a[i][k] * b[k][j]) in matrix multiplication program.

查看:565
本文介绍了为什么我们在矩阵乘法程序中使用c [i] [j] = c [i] [j] +(a [i] [k] * b [k] [j])。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j, k;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = 0
     for(k = 0; k < 3; k++)
     {
        c[i][j] = c[i][j] + (a[i][k] * b[k][j])
     }
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}

推荐答案

因为公式只是这样。因此,您将矩阵A的第一行的每个项目与矩阵B的第一列的每个项目相加,并将它们的乘积相加以得到总的乘积值。



所以a [ i] [k]会给你第一行的Matrix 1,因为k正在增加。所以在那个内部循环中,你将乘以[0] [0] * b [0] [0]。接下来,对于k = 1,它将是[0] [1] * b [1] [0]。等等。所以b按列增加,a按行增加。



您将该产品存储在另一个矩阵中C.



对于公式,请参阅 - 矩阵乘法 [ ^ ]。
Because the formula is like that only. So you multiple each item of first row of Matrix A with each item of first column of Matrix B and sum their product to get the total multiplied value.

So a[i][k] will give you first row items of Matrix 1 as k is increasing. So in that inner loop you will multiply a[0][0] * b[0][0]. Next for k=1, it will be a[0][1] * b[1][0]. And so on. So b is increasing by column and a is increasing by row.

You are storing that product in another matrix C.

For formula, refer - Matrix Multiplication[^].


这篇关于为什么我们在矩阵乘法程序中使用c [i] [j] = c [i] [j] +(a [i] [k] * b [k] [j])。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆