【问题标题】:Why did I need to use this statement twice - Matrix Multiplication为什么我需要两次使用这个语句 - 矩阵乘法
【发布时间】:2012-08-24 10:26:36
【问题描述】:

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

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

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

我使用的方法非常有效,但直到我使用此语句两次之前它并没有给我正确的答案。 (我通过试错法得到了这个)。

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

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

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

有什么方法可以在不破坏应用程序的情况下摆脱其中一个语句?

【问题讨论】:

  • 如果写两次,就相当于matrixAns[r][s] = 2*e+matrixAns[r][s]。这是你想要的吗?
  • 我想知道你是如何让 C++ 制作非常量值的数组。
  • 这样更灵活,因为它可以找到满足基本条件的任意矩阵的倍数!
  • @mikeTheLiar 不幸的是,这是一个默认启用的 g++ 扩展。
  • 顺便说一句,大多数程序员会使用matrixAns[r][s] += e;

标签: c++ arrays matrix-multiplication


【解决方案1】:

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

您的代码只取 elast 乘法的乘积 - matrix[r][b - 1] * matrixB[b - 1][s] - 并丢弃前 N-1 个乘积。添加e - 最后一个乘法 - 一次、两次或 3 次都是不正确的,尽管它可能似乎适用于某些输入。

你的答案循环,带有 cmets:

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

【讨论】:

    【解决方案2】:

    您是否打印了 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])
    

    【讨论】:

    • 如果他得到正确的乘法答案,那是不可能的。
    • 也许吧。 for(int t=0; t
    • 我已经为我初始化这个 matrixAns 的地方添加了一条评论 .. 哼哼反正它持有 initail 值!
    • @abhejit.rajagopal 用于初始化矩阵!
    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 2012-01-18
    • 2016-02-29
    • 2018-11-22
    • 2019-04-09
    • 2011-03-14
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多