m1*m2=m其中是一个x行z列的一个矩阵,例如
代码如下:
#include
void matrix_multiply(int *m1, int *m2, int *m, int x, int y, int z)
{
int row = 0; //m矩阵的行
int col = 0; //m的列
int k = 0;
int *m1p = m1;
int *m2p = m2;
for (row = 0; row < x; row++)
{
for (col = 0; col < z; col++)
{
*m = 0;
m1p = m1 + y*row;
m2p = m2+col;
while (k < y)
{
*m += *m1p * *m2p;
m1p++;
m2p+=z;
k++;
}
k = 0;
m++;
}
}
}
int main()
{
int m1[3][2] = {
{ 2, -6 },
{ 3, 5 },
{ 1, -1 } };
int m2[2][4] = {
{ 4, -2, -4, -5 },
{ -7, -3, 6, 7 }
};
int m[3][4] = { 0 };
int i = 0;
int j = 0;
matrix_multiply(m1, m2, m, 3, 2, 4);
for (i = 0; i <3; i++)
{
for (j = 0; j < 4; j++)
printf("%4d", m[i][j]);
printf("\n");
}
system("pause");
return 0;
}
时间: 11-23