为什么我需要使用两次这个语句——矩阵乘法

Why did I need to use this statement twice - Matrix Multiplication

本文关键字:语句 两次 为什么      更新时间:2023-10-16

我正在开发一个程序来寻找矩阵乘法。

#include <iostream>
using namespace std;
int main()
{
    int a=0,b=0,c=0,d=0,e=0;
    cout<<"Enter the order of the first matrix  A  nnNumber of Rows : ";
    cin>>a;
    cout<<"nNumber of Columns : ";
    cin>>b;
    cout<<endl;
    int matrixA[a][b];
    cout<<"Enter the matrix Elements "<<endl;
    for(int m=0; m<a; m++)
    {
        for(int n=0; n<b; n++)
        {
            cout<<"A ("<< m+1 <<" , "<<n+1<<" ) =";
            cin>>matrixA[m][n];
            //cout<<",";
        }
        cout<<endl;
    }
//////////////////////////  Startup
    cout<<"Enter the order of the Second matrix  A  nnNumber of Rows : "<<b;
    c=b;
    cout<<"nNumber of Columns : ";
    cin>>d;
    cout<<endl;
    int matrixB[c][d];
    cout<<"Enter the matrix Elements "<<endl;
    for(int p=0; p<c; p++)
    {
        for(int q=0; q<d; q++)
        {
            cout<<"B ("<< p+1 <<" , "<<q+1<<" ) =";
            cin>>matrixB[p][q];
            //cout<<",";
        }
        cout<<endl;
    }
    ///////////// initialisting matrixAns
    int matrixAns[a][d];
    for(int p=0; p<a; p++)
    {
        for(int q=0; q<d; q++)
        {
            matrixAns[p][q]=0;    
        }    
    }
////////////////////  finding ans
    for(int r=0; r<a; r++)
    {
        for(int s=0; s<d; s++)
        {
            for(int t=0; t<b; t++)
            {    
                e = matrixA[r][t]*matrixB[t][s];       
            }
            matrixAns[r][s] = e+matrixAns[r][s];
            matrixAns[r][s] = e+matrixAns[r][s];   //dont know why i have to add this same code again
    }
    }
////////////////////// Ans Printing    
    cout<<"nMatrix Multiplication Answer  n"<<endl;
    for(int h=0; h<a; h++)
    {    
        for(int i=0; i<d; i++)
        {    
            cout<<" "<<matrixAns[h][i]<<" ";    
        }
        cout<<endl;
    }    
}

还有一件重要的事:这不是我的家庭作业或作业!

我使用的方法非常有效,但直到我使用了两次这个语句,它才给我正确的答案。(我是通过反复试验的方法得到的)。

matrixAns[r][s] = e+matrixAns[r][s];  

我还通过使用循环初始化了matrixAns(并将其设置为0)。

我是C++的新手,想知道我犯了什么错误,以及使用这两个语句是如何给我正确答案的。

有没有办法在不破坏应用程序的情况下删除其中一条语句?

在计算答案时,您没有正确地进行点积运算。您需要将两个矩阵之间的单个单元格相乘,然后将答案中单个单元格的所有乘积相加。

您的代码只取最后一次乘法的乘积matrix[r][b - 1] * matrixB[b - 1][s]-的e,并丢弃第一个N-1乘积。将e(最后一次乘法)加一次、两次或三次都是不正确的,尽管它可能会出现来处理某些输入。

你的回答循环,带评论:

for(int r=0; r<a; r++)
{
    for(int s=0; s<d; s++)
    {
        for(int t=0; t<b; t++)
        {
            e = matrixA[r][t]*matrixB[t][s];
        }
        // now e only has the value from that final multiplication, of
        //    matrix[r][b - 1] * matrixB[b - 1][s]. All of the other
        //    products were lost.
        // so now it doesn't matter how many times you add e, you'll
        //    get the wrong product:
        matrixAns[r][s] = e+matrixAns[r][s];
        matrixAns[r][s] = e+matrixAns[r][s];
    }
}

将您的回答循环更改为:

for(int r=0; r<a; r++)
{
    for(int s=0; s<d; s++)
    {
        for(int t=0; t<b; t++)
        {
            e = matrixA[r][t]*matrixB[t][s];
            // accumulation of the products should be INSIDE the loop:
            matrixAns[r][s] = matrixAns[r][s] + e;
        }
    }
}

您是否打印了matrixAns[r][s]以确保它能容纳一些东西?

听起来matrixAns[r][s]是空的;这就是我认为正在发生的事情:

matrixAns[r][s] = e+matrixAns[r][s]; //stores e in matrixAns[r][s]
matrixAns[r][s] = e+matrixAns[r][s]; //adds e to e (whats in matrixAns[r][s])