MEX 编程矩阵乘以矩阵的具体代码
时间: 2023-11-28 17:05:24 浏览: 124
以下是使用 C++ 语言实现的矩阵相乘的代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> multiply(vector<vector<int>> A, vector<vector<int>> B) {
int m = A.size(), n = A[0].size(), p = B[0].size();
vector<vector<int>> C(m, vector<int>(p, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < p; ++j) {
for (int k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
int main() {
vector<vector<int>> A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<int>> B = {{10, 11}, {12, 13}, {14, 15}};
vector<vector<int>> C = multiply(A, B);
for (auto row : C) {
for (auto val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
```
该程序使用两个 vector<vector<int>> 类型的矩阵 A 和 B 作为输入,输出矩阵 C 存储两个矩阵的乘积。函数 multiply() 使用三个循环来计算矩阵 C 的每个元素。程序最终将矩阵 C 打印到屏幕上。
阅读全文
相关推荐
















