这是我编写的代码,我试过但它只适用于相同的长度矩阵,它应该适用于第一个的列作为第二个行的两个矩阵..
void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int r1,intc2,int r2)
{
int i , j,k,sum;
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
sum = 0;
for(k=0; k<r2; k++)
{
sum += matrixA[i][k] * matrixB[k][j];
}
matrixC[i][j]=sum;
sum=0;`
}
}
答案 0 :(得分:0)
所以假设您正在寻找天真的算法:
这是用c#
写的 public float[,] MatrixMultiply(float[,] leftMat, float[,] rightMat)
{
// get the required row and column ranks for both matrices
int leftRowNum = leftMat.GetLength(0);
int rightRowNum = rightMat.GetLength(0);
int leftColNum = leftMat.GetLength(1);
int rightColNum = rightMat.GetLength(1);
// Check that the inner dimensions are the same
if(leftColNum != rightRowNum)
return null;
int innerLen = leftColNum;
// now define resulting matrix dimensions
float[,] result = new float[leftRowNum,rightColNum];
// perform the multiplication using 3 nested for loops
for(int i = 0; i < leftRowNum; i++)
{
for(int j = 0; j < rightColNum; j++)
{
for(int inner = 0; inner < innerLen; inner++)
{
result[i,j]+= leftMat[i,inner]*rightMat[inner,j];
}
}
}
return result;
}
现在您的代码将是:
void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int r1,intc2,int r2)
float[,] result = MatrixMultiply(MatrixMultiply(matrixA,matrixB),matrixC);
也可写:
float[,] result = MatrixMultiply(matrixA,MatrixMultiply(matrixB,matrixC));
给定矩阵乘法的关联属性。