非平方矩阵乘法帮助C++

Non Square Matrix Multiplication Help C++

本文关键字:帮助 C++ 方矩阵      更新时间:2023-10-16

首先,很抱歉代码有点乱,因为我在处理不同的事情以使其正常工作。到目前为止,我的代码可以很好地乘以平方矩阵;然而,它在计算非平方矩阵方面存在困难。调试后,我的最佳猜测是如何重新调整向量的大小,并且存在导致程序崩溃的越界错误。如果有任何帮助,我们将不胜感激,我的代码应该能够在矩阵乘法规则中乘以任何向量大小。

我还想注意的是,这是一个硬件任务,所以我只限于如何构建我的代码,基本上只使用向量,不能编写自己的类等。…

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

初始化您的第一个矩阵,如下所示:

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

你的第二个是这样的:

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

其中CCD_ 1。注意,不存在暗示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()

但正如在评论中已经指出的那样,最好使用vector::size()而不是将大小作为附加参数传递。

此外,如果你乘以(2x3)(3x2),结果是(2x2):

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

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也有同样的问题。你应该做的是确保你的循环使用rows2 = cols0的返回值,但你没有这样做

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

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

因此,您要么需要确保matrix1和matrix2的大小能够容纳行/列的数量,要么对这些循环进行节流以使用最小的行/列。

总的来说,问题是在我看到的代码中没有任何地方使用vector::size()。因此,开始使用size()对您有利——不要创建多余的(可能是错误设置的)变量,这些变量应该表示行和列的大小。