简体   繁体   English

在C ++中使用2d向量乘法矩阵

[英]Multiplying Matrices using 2d Vectors in C++

I'm trying to design a program that creates a matrix using vectors of vectors of integers, and then multiplyies it with another matrix. 我正在尝试设计一个程序,该程序使用整数向量的向量创建矩阵,然后将其与另一个矩阵相乘。 I know how to multiply matrices on paper, but when I try to implement it in my program, I'm not getting it to work. 我知道如何在纸上将矩阵相乘,但是当我尝试在程序中实现矩阵时,却无法正常工作。 I know that both matrices are entered correctly and are passed correctly as I have the the output of those functions so that I can debug. 我知道两个矩阵都正确输入并正确传递,因为我具有这些函数的输出,以便可以调试。 The program works incorrectly when I try to multiply them. 当我尝试将它们相乘时,该程序无法正常工作。 The answer and the number of elements are not right. 答案和要素数量不正确。 I know I'm missing something but can't figure out what. 我知道我遗漏了一些东西,但无法弄清楚是什么。

Matrix Matrix::operator*(Matrix m){
    vector<int> mRow = m.getRow(0);
    vector<int> mCol = m.getCol(0);
    vector<int> newElem;
    int product = 0;

    //adds the contents of the 2nd matrix to the 2d vector
    vector< vector<int> > m2(mRow.size(), vector<int>(mCol.size()));        
    for (int i = 0; i < mRow.size(); i++){
        mRow.clear();
        mRow = m.getRow(i);
        for (int j = 0; j < mCol.size(); j++){
            m2[j][i] = mRow[j];
        }

    }

    //Multiplies the matrices using the 2d matrix**THIS IS WHERE IT GOES WRONG**
    for (int i = 0; i < row; i++){
        for (int j = 0; j < column; j++){
            product += matrix[i][j]*m2[j][i];
        }
        newElem.insert(newElem.begin()+i,product);
        product = 0;
    }

    //displays the products so that i can see if its working
    for (int i = 0; i < newElem.size(); i++){
        cout << "  "<<newElem[i]<<endl;
    }

    //adds the new product vector to a new Matrix object and returns it
    Matrix newM(row, mCol.size());
    vector<int> temp;
    for (int i = 0; i < row; i++){
        for (int j = 0; j < mCol.size(); j++){
            temp.insert(temp.begin()+j, newElem[0]);
            newElem.erase(newElem.begin()); 
        }
        newM.setRow(temp,i);
        temp.clear();
    }
    return newM;
}

Although I don't know whether this helps, I'm using this site as a reference for multiplying 2 matrices together. 尽管我不知道这是否有帮助,但我还是以此网站为参考将2个矩阵相乘。

Your matrix representation has nothing to do with your mistake. 您的矩阵表示形式与您的错误无关。 You need to have more nested iterations. 您需要具有更多的嵌套迭代。 Think of a result matrix and iterate through that to calculate it's every element. 考虑一个结果矩阵,并对其进行迭代以计算出每个元素。 In a pseudocode: 用伪代码:

for i in result column
 for j in result row
   res[i, j] = multiply(m1, m2, i, j)

where multiply function is the nested loop, something like this: 其中乘法函数是嵌套循环,如下所示:

multiply(m1, m2, i, j)
{
  val = 0;
  for k in row
    val += m1[i, k] * m2[k, j]
 return val
}

Here is an implementation of the outer loops. 这是外循环的实现。 Mind you, there are no error checking in the code. 请注意,代码中没有错误检查。

vector<vector<int> > ml;
vector<vector<int> > mr;
// fill in ml and mr
...

// result matrix
vector<vector<int> > res;

// allocate the result matrix
res.resize(ml.size());
for( it = res.begin(); it != res.end(); ++it)
  it->resize(ml[0].size());


// loop through the result matrix and fill it in
for( int i = 0; i < res.size(); ++i)
    for( int j = 0; j < res[0].size(); ++j)
       res[i][j] =  multiply(ml, mr, i, j);

Leaving a proper implementation of multiply() function to you. 留给您正确的乘法()函数实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM