標題:
矩陣乘法模擬
[打印本頁]
作者:
呂晉豪
時間:
2024-7-29 19:05
標題:
矩陣乘法模擬
#include <bits/stdc++.h>
using namespace std;
int a[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
int b[2][4] = {
{7, 8, 9, 10},
{11, 12, 13, 14}
};
int ans[3][4];
void output(int idx1, int idx2) {
int n1 = 3, m1 = 2, n2 = 2, m2 = 4;
cout << "a矩陣:" << endl;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
cout << endl;
cout << "b矩陣:" << endl;
for (int i = 0; i < n2; i++) {
for (int j = 0; j < m2; j++) {
cout << b[i][j] << "\t";
}
cout << endl;
}
cout << endl;
cout << "ans矩陣:" << endl;
for (int i = 0; i < 3; i++) {
int flag = 1;
for (int j = 0; j < 4; j++) {
if (i == idx1 and j == idx2) {
flag = 0;
break;
}
cout << ans[i][j] << " ";
}
if (!flag) break;
cout << endl;
}
cout << endl;
cout << "現在要算ans[" << idx1 << "][" << idx2 << "]" << endl;
cout << "ans[" << idx1 << "][" << idx2 << "]等於:" << endl;
for (int j = 0; j < 2; j++) {
cout << a[idx1][j] << "\t";
}
cout << endl;
cout << "內積" << endl;
for (int i = 0; i < 2; i++) {
cout << b[i][idx2] << endl;
}
cout << endl;
system("pause");
system("cls");
}
int main()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
output(i, j);
int sum = 0;
for (int r = 0; r < 2; r++) {
sum += a[i][r] * b[r][j];
}
ans[i][j] = sum;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << ans[i][j] << "\t";
}
cout << endl;
}
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2