作为程序的一部分,我需要将两个二维矩阵相乘。这些矩阵是创建的矩阵类的一部分。目前我的代码运行良好,但我想知道是否有更有效的方法将这些矩阵相乘在一起。
public Matrix multiply(Matrix matrix)
{
int[][] userMatrix = matrix.getMatrix(); //returns 2D array of Matrix matrix object
int[][] multiplied = new int[length][length]; //int [][] for the multiplied matrix
int[] tempA = new int[length];
int[] tempB = new int[length];
int sum = 0;
for (int row = 0; row < length; row++)
{
for (int col = 0; col < length; col++)
{
tempA[col] = arrayObject[row][col];
}
for (int j = 0; j < length; j++)
{
for (int i = 0; i < length; i++)
{
tempB[i] = userMatrix[i][j];
}
for (int k = 0; k < length; k++)
{
sum += tempA[k] * tempB[k];
}
multiplied[row][j] = sum;
sum = 0;
}
}
Matrix returnMatrix = new Matrix(multiplied, multiplied.length); //converts the int[][] to a Matrix object
return returnMatrix;
}
谢谢您!
暂无答案!
目前还没有任何答案,快来回答吧!