当前位置:   article > 正文

c++使用vector求两个矩阵相乘_c++ vector乘法

c++ vector乘法

c++使用vector求两个矩阵相乘

  • 直接上代码
#include <vector>
#include <iostream>
using namespace std;


vector<vector<double>> mutil(vector<vector<double>> m1, vector<vector<double>> m2) {
    //两矩阵相乘
    int m = m1.size();
    int n = m1[0].size();
    int p = m2[0].size();
    vector<vector<double>> array;
    vector<double> temparay;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < p; j++) {
            double sum = 0;
            for (int k = 0; k < n; k++) {
                sum += m1[i][k] * m2[k][j];
            }
            temparay.push_back(sum);
        }
        array.push_back(temparay);
        temparay.erase(temparay.begin(), temparay.end());
    }
    return array;
}


int main() {
    vector<vector<double>> nums1 = {
        {1, 2, 3},
        {4, 15, 6},
        {7, 8, 8}
    };
    vector<vector<double>> w = mutil(nums1, nums1);
    for (int i = 0; i < w.size(); i++)
    {
        for (int j = 0; j < w[0].size(); j++)
        {
            cout << w[i][j] << " ";
        }
        cout << endl;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/818488
推荐阅读
  

闽ICP备14008679号