【问题标题】:Non Square Matrix Multiplication Help C++非方阵乘法帮助 C++
【发布时间】:2014-09-24 09:54:56
【问题描述】:

首先抱歉代码有点混乱,因为我正在摆弄不同的东西来尝试让它工作。到目前为止,我的代码可以很好地乘方矩阵;但是,它在计算非方阵方面存在困难。调试后我最好的猜测是我如何重新调整向量的大小,并且存在导致程序崩溃的越界错误。任何帮助将不胜感激,我的代码应该能够使用矩阵乘法规则乘以任何向量大小。

我还想指出这是一个硬件分配,所以我仅限于如何构建我的代码,基本上只能使用向量,不能编写自己的类等......

#include <iostream>
#include <vector>
using namespace std;

void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2); 
void setMatrix(vector <vector<int> > &matrix, int rows, int cols);

int main()
{
    int rows, cols, rows2, cols2;    
    vector< vector<int> > matrix, matrix2;        
    cout<<"Please enter the number of Rows and Columns for your first Matrix."<<endl;
    cout<<"Rows: ";
    cin>>rows;
    cout<<"Columns: ";
    cin>>cols;

    matrix.resize(cols, vector<int>(rows,0));  //Saw this online so not sure how it works but it works, if i take out one i cant do row<column and vice versa
    matrix.resize(rows, vector<int>(cols,0));

    cout<<"Size has been declared, please enter data for your matrix"<<endl;

    setMatrix(matrix,rows,cols);

    cout<<"Second Matrix Automatically Set by Matrix Multiplication Rule"<<endl; //Just automatically sets second matrix as per Matrix Multiplication Rule
    rows2=cols;
    cols2=rows;
    cout<<"Second Matrix Size is: " << rows2 << " by " << cols2 << endl;
    matrix2.resize(cols2, vector<int>(rows2,0));
    matrix2.resize(rows2, vector<int>(cols2,0));

    setMatrix(matrix2,rows2,cols2);        

    cout<<"Multiplied Matrix is:"<<endl;
    multiply_matrices(matrix,matrix2,cols,rows2);

    system("PAUSE");
    return 0;
}

void setMatrix(vector <vector<int> > &matrix, int rows,int cols){
     int num;
     for(int i = 0; i < rows; i ++)
     {
        for (int j = 0; j < cols; j++)
        {
            cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": ";
            cin>>num;
            matrix[i][j]=num;            
        }        
        cout <<  endl;
    }

 /*for(int i = 0; i < rows; i ++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << matrix[i][j] << " ";
        }       
        cout <<  endl;
    }          
  */   
     }
void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2){
    vector< vector<int> > tempMatrix;
    int newrows=rows2;
    int newcols=cols;
    int sum;
    tempMatrix.resize(newcols, vector<int>(newrows,0));   //Resizing new matrix to proper size, so if it was (2x3)(3x2), new matrix is (3x3)

    for (int i = 0; i < newrows; i++)                    //This Works Fine for Square Matrixes but not for others, i have no clue how to fix it?
    {
        for (int j = 0; j < newcols; j++){
            //sum=0;    
            for (int u = 0; u < newcols; u++)
            {
                //sum+=matrix1[i][u] * matrix2[u][j];
                //tempMatrix[i][j]=sum;
                tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j];
            }
        }
    }
    for(int i = 0; i < newrows; i ++)
    {
        for (int j = 0; j < newcols; j++)
        {
            cout << tempMatrix[i][j] << " ";
        }        
        cout <<  endl;
    }          
}

【问题讨论】:

  • 好吧,让代码更安全的一种方法是使用 vector::size() 来限制循环,而不是使用容易具有错误最大值的变量。更好的是,使用从 begin() 到 end() 的迭代器。
  • I also would like to note this is a HW assignment so I am limited to how I can build my code, basically ONLY using vectors, cannot write your own class etc.... 哇。最后是一个实际允许您使用 C++ 的硬件分配。通常我们在 SO 上得到相反的结果,学生不能使用vector
  • 是的,我对二维向量很陌生,所以我不知道检查二维数组大小的正确格式实现。
  • 为什么需要将rowscols传递给setMatrix?向量知道自己的大小,而不必传递这些值。除非您需要在开始输入循环之前先调整矩阵的大小?
  • 它用于 for 循环,我再次不熟悉如何使用 2D 数组大小实现和适当的比较,所以我更容易使用行和列来遍历矩阵。跨度>

标签: c++ matrix vector multiplication


【解决方案1】:

像这样初始化你的第一个矩阵:

matrix.resize(rows, vector<int>(cols,0));

你的第二个是这样的:

matrix2.resize(rows2, vector<int>(cols2,0));

在哪里rows2 = cols。请注意,没有暗示cols2 == rows 的“乘法规则”。

问题出在你的 multiply_matrices 函数中,循环应该在哪里

for (int i = 0; i < rows; i++) // or matrix1.size()
for (int j = 0; j < cols2; j++) // or tempMatrix[i].size()
for (int u = 0; u < cols; u++) // or rows2 or matrix1[i].size()

但正如 cmets 中所述,最好使用 vector::size() 而不是将大小作为附加参数传递。

另外,如果将 (2x3)(3x2) 相乘,则结果为 (2x2):

tempMatrix.resize(rows, vector<int>(cols2,0));   

【讨论】:

  • 是的,谢谢,应该是 2x2 倒过来谢谢。请大家多多指教,谢谢。
  • 老兄,谢谢!测试它,到目前为止它工作得很好!基本上是逻辑问题。但如果您能向我展示使用 vector::size() 的循环,我将不胜感激,这样我将来就可以正确实现二维数组循环。谢谢
【解决方案2】:

resize() 函数没有任何问题。可能有问题的是您忽略了最大大小并仅依赖于传递给您的函数的变量。

例如,您的setMatrix 函数传递了rowscols,但这不是必需的。

应该只使用矩阵来重写函数以提供循环的大小:

void setMatrix(vector<vector<int> > &matrix)
{
     int num;
     for(int i = 0; i < matrix.size(); ++i)
     {
        for (int j = 0; j < matrix[i].size(); ++j)
        {
            cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": ";
            cin>>num;
            matrix[i][j] = num;            
        }        
        cout <<  endl;
    }
}

multiply_matrix 也有同样的问题。你应该做的是确保你的循环使用vector::size() 的返回值,你不这样做。问题出在这里:

for (int i = 0; i < newrows; i++)    
{
    for (int j = 0; j < newcols; j++)
    {
        for (int u = 0; u < newcols; u++)
        {
            tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j];

您将tempMatrix 的大小调整为newrows 行和newcols 列。但是你怎么知道matrix1matrix2 至少有newrows 行和newcols 列?你不知道,但你只是假设他们知道。

因此,您要么需要确保 matrix1 和 matrix2 的大小可以容纳行/列的数量,要么限制这些循环以使用最少的行/列。

总的来说,问题是我看到的代码中没有任何地方使用vector::size()。所以开始使用size() 来发挥你的优势——不要创建多余的(并且可能是错误设置的)变量,这些变量应该表示行和列的大小。

【讨论】:

  • 这对于 (int i = 0; i
  • oi 对不起,只是从第一个开始复制粘贴,网站功能也很糟糕-_-
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2023-03-28
相关资源
最近更新 更多