![](/img/trans.png)
[英]C++ Matrix Multiplication - understanding the logic behind an optimized method for it
[英]Matrix Multiplication ¿logic error in cicle? (C++ OOP)
我正在嘗試編寫一個用於乘法矩陣的代碼,這是“ for”子彈試圖更改所得矩陣(C)的值時出現的普遍問題。
如果嘗試執行此代碼,引入2個矩陣A和B(均為2x2),則結果將顯示結果矩陣C僅顯示第一個值C [0] [0]。
我已經嘗試驗證方法“ mult(Matriz A,Matriz B)”,但是有些地方一定是錯誤的,請您幫我找出cicle中的錯誤內容。
#include <iostream>
#include <stdio.h>
#define TAM 10
using namespace std;
class Matriz
{
private:
int M[TAM][TAM];
int n, m;
public:
Matriz ()
{
n = m = 1;
}
Matriz (int n, int m)
{
this->n = n;
this->m = m;
}
void ingMatriz(char c)
{
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
cout<<c<<"["<<i+1<<"]["<<j+1<<"]=";
cin >>M[i][j];
}
}
void muestraMatriz()
{
for (int i=0; i<n; i++)
{
cout << "\n";
for (int j=0; j<m; j++)
cout<<" "<<M[i][j];
}
}
void mult (Matriz A, Matriz B)
{
int an=A.n;
int am=A.m;
int bm=B.m;
for(int a=0; a<an; a++) //This is the general cicle
for(int b=0; b<bm; b++)
for(int c=0; c<am; c++)
M[a][b] += (A.M[a][c] * B.M[c][b]);
}
};
int main()
{
cout << "\nxxxxxx Multiplicacion de matrices xxxxxx";
int f1,c1,f2,c2;
cout<< "\n \n* El numero de columnas de la 1er matriz(A) debe ser igual a numero de filas de la segunda matriz(B) *";
cout<< "\n \nIngresa el numero de FILAS de la 1er matriz(A): ";
cin >> f1;
cout<< "\nIngresa el numero de COLUMNAS de la 1er matriz(A): ";
cin >> c1;
cout<< "\nIngresa el numero de FILAS de la 2da matriz(B): ";
cin >> f2;
cout<< "\nIngresa el numero de COLUMNAS de la 2da matriz(B): ";
cin >> c2;
if (c1==f2)
{
Matriz A(f1,c1);
cout << "\nIngresa los valores de la matriz 'A': \n\n";
A.ingMatriz('A');
Matriz B(f1,c2);
cout << "\n \n********************************************\nIngresa los valores de la matriz 'B': \n \n";
B.ingMatriz('B');
Matriz C;
C.mult(A,B);
cout << "\nMatriz A: ";
A.muestraMatriz();
cout << "\nMatriz B: ";
B.muestraMatriz();
cout << "\nMultiplicacion AB: ";
C.muestraMatriz();
}
else
cout << "\n \n \n El numero de columnas de la 1er matriz(A) debe ser igual al numero de filas de la segunda matriz(B) ";
getchar();
getchar();
return 0;
};
我在main中做了一些更改,如您在圖片中看到的,我將對象C更改為Matriz C(f1,c2),因此它可以為所得矩陣設置值,但仍然顯示垃圾。 這是輸出的圖片:
http://i.stack.imgur.com/oTdK7.png
最終代碼:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#define TAM 10
using namespace std;
class Matriz
{
private:
int M[TAM][TAM];
int n, m;
public:
Matriz ():n(1), m(1)
{
for (int i=0; i<TAM; i++)
for (int j=0; j<TAM; j++)
M[i][j] = 0;
}
Matriz (int n, int m):n(n), m(m)
{
for (int i=0; i<TAM; i++)
for (int j=0; j<TAM; j++)
M[i][j] = 0;
}
void ingMatriz(char c)
{
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
cout<<c<<"["<<i+1<<"]["<<j+1<<"]=";
cin >> M[i][j];
}
}
void muestraMatriz()
{
for (int i=0; i<n; i++)
{
cout << "\n";
for (int j=0; j<m; j++)
cout<<" "<<M[i][j];
}
}
void mult (Matriz A, Matriz B)
{
int an=A.n;
int am=A.m;
int bm=B.m;
for(int a=0; a<an; a++)
for(int b=0; b<bm; b++)
for(int c=0; c<am; c++)
M[a][b] += (A.M[a][c] * B.M[c][b]);
}
};
int main()
{
while (0<1){
cout << "xxxxxx Multiplicacion de matrices xxxxxx";
int f1,c1,f2,c2;
cout<< "\n \n* El numero de columnas de la 1er matriz(A) debe ser igual al numero de filas de la segunda matriz(B) *";
cout<< "\n \nIngresa el numero de FILAS de la 1er matriz(A): ";
cin >> f1;
cout<< "\nIngresa el numero de COLUMNAS de la 1er matriz(A): ";
cin >> c1;
cout<< "\nIngresa el numero de FILAS de la 2da matriz(B): ";
cin >> f2;
cout<< "\nIngresa el numero de COLUMNAS de la 2da matriz(B): ";
cin >> c2;
if (c1==f2)
{
Matriz A(f1,c1);
cout << "\nIngresa los valores de la matriz 'A': \n\n";
A.ingMatriz('A');
Matriz B(f2,c2);
cout << "\n \n********************************************\nIngresa los valores de la matriz 'B': \n \n";
B.ingMatriz('B');
Matriz C(f1,c2);
C.mult(A,B);
cout << "\n\nMatriz A: ";
A.muestraMatriz();
cout << "\n\nMatriz B: ";
B.muestraMatriz();
cout << "\n\nMultiplicacion AB: ";
C.muestraMatriz();
}
else
cout << "\n \n \n El numero de columnas de la 1er matriz(A) debe ser igual al numero de filas de la segunda matriz(B) ";
getchar();
getchar();
system("cls");
}
return 0;
};
告訴我您主要功能的作用對我來說有些棘手,因為我不是母語為西班牙語的人=)
但問題似乎在於您正在使用默認構造函數初始化C,該構造函數將其寬度和高度都初始化為1。 然后,當您嘗試將其他兩個2 * 2矩陣相乘時,相乘的索引永遠不會超過1和1-即使A和B都具有n = 2
和m = 2
,因為C僅具有n = m = 1
(在其構造函數中分配),它只乘以第一個元素,然后退出兩個循環。
為了獲得所需的內容,首先需要將C初始化為適當的尺寸,如下所示:
Matriz C(2, 2)
但是,這很快就會令人討厭,因為您已經知道輸出矩陣的大小應該是-因此,在您的矩陣乘法代碼中,您可以分別設置m = Am
和n = Bn
,因為假定乘法的結果矩陣具有第一矩陣的寬度和第二矩陣的高度。 然后,您只需要確保在乘以=)時傳入正確大小的矩陣即可。
我看到的第二個問題是,在進行乘法運算之前,您沒有將C的值初始化為任何值-本質上來說,乘法運算是正確的,但是由於其中已經有未初始化的值,因此在添加乘法運算時,垃圾+ x =更多垃圾。 編輯:完成之前的意外張貼。 您要更改的行M[a][b] += (AM[a][c] * BM[c][b]);
到M[a][b] = (AM[a][c] * BM[c][b]);
-這樣一來,您可以將值正確設置為應有的值,而不是添加到已存在的值上。
EDIT2:您不必在乘法代碼本身中進行初始化(請參見注釋),而應在構造函數中使用循環進行初始化:
public:
Matriz ():n(1), m(1) //also note the initializer lists instead of doing it in the body of the ctor
{
for (int i = 0; i < TAM; ++i)
for (int j = 0; i < TAM; ++j)
M[i][j] = 0;
}
Matriz (int n, int m):n(n), m(m) //lets you do this instead of explicitly dereferencing this (this->n = n, etc) --
//also, more efficient generally (though maybe not since your data is only ints in this case anyways)
{
for (int i = 0; i < TAM; ++i)
for (int j = 0; i < TAM; ++j)
M[i][j] = 0;
}
由於您要乘以2個矩陣:An = Bm,因此您的代碼應更像:
void mult (Matriz A, Matriz B)
{
for(k=0;k<A.m;k++)
{
for(i=0;i<A.n;i++)
{
M[i][k] = 0;
for(j=0;j<B.m;j++)
M[k][i] += A[k][j] * B[j][i]
cout << "Matriz c:" << M[k][i]<<" ";
}
cout <<"\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.