为什么我需要使用这个语句两次 - 矩阵乘法 [英] Why did I need to use this statement twice - Matrix Multiplication

查看:147
本文介绍了为什么我需要使用这个语句两次 - 矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was developing a program to find matrix multiplication.

#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  \n\nNumber 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  \n\nNumber 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;
    }    
}

一个重要的事情:这不是我的家庭作业赋值!

And one important thing: this is not my homework or assignment!

我使用的方法完美地工作,但它不给我正确的答案,直到我使用这个语句两次。 (我通过一个试验和错误的方法得到这个)。

The method that I have used works perfectly but it does not give me the right answer until I use this statement twice. (I got this by a trial and error method).

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

我还使用了 matrixAns 一个循环(并将它设置为0)。

I have also initailised the matrixAns by using a loop (and had set it to 0).

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

I am new to C++ and want to know what was the error that I got and how the use of the two statements gives me correct answer.

有没有办法摆脱其中一个语句而不销毁应用程序?

Is there any way to get rid of one of the statements without destroying the app?

推荐答案

在计算答案时,您不是正确地使用点积。您需要将两个矩阵之间的单个单元格相乘,然后对答案中单个单元格的全部求和。

You're not doing the dot products correctly when calculating your answer. You need to multiply individual cells between the two matrices, then sum all those products for an individual cell in your answer.

乘以乘法 - 矩阵[r] [b-1] *矩阵B的 e b-1] [s] - 并舍弃第一个 N-1 产品。添加 e - 这最后一次乘法 - 一次,或两次或3次都是不正确的,虽然可能会出现以处理某些输入。

Your code is only taking the product, in e, of the last multiplication - matrix[r][b - 1] * matrixB[b - 1][s] - and discarding the first N-1 products. Adding e - this last multiplication - once, or twice or 3 times are all incorrect, though it may appear to work with certain inputs.

您的回答循环,包含注释:

Your answer loop, with comments:

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];
    }
}

将您的回答循环更改为:

Change your answer loop to:

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;
        }
    }
}

这篇关于为什么我需要使用这个语句两次 - 矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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