1
我想讓矩陣乘法工作,我只是開始學習編程。我如何將值添加到我在主要創建的4x4和4x4矩陣中? (這不是我的代碼,但我瞭解大部分,除了使用setElement & getElement的,如果您能向我解釋什麼它應該做的),我真的很感激幫助Java,矩陣乘法,我如何將值插入矩陣
public class Matrix{
private float[][] elements;
private int rows;
private int cols;
public int getRows()
{
return rows;
}
public int getCols()
{
return cols;
}
public Matrix(int rows, int cols)
{
this.rows = rows;
this.cols = cols;
elements = new float[rows][cols];
}
public void setElement(int row, int col, float value)
{
elements[row][col] = value;
}
public float getElement(int row, int col)
{
return elements[row][col];
}
public static Matrix mult(Matrix a, Matrix b)
{
Matrix c = new Matrix(a.getRows(), b.getCols());
for (int row = 0; row < a.getRows(); row++)
{
for (int col = 0; col < b.getCols(); col++)
{
float sum = 0.0f;
for (int i = 0; i < a.getCols(); i++)
{
sum += a.getElement(row, i) * b.getElement(i, col);
}
c.setElement(row, col, sum);
}
}
return c;
}
public static void main(String[] args)
{
Matrix m = new Matrix(4,4);
Matrix m1 = new Matrix(4,4);
Matrix multip = Matrix.mult(m, m1);
multip = Matrix.mult(m, m1);
System.out.println(multip);
}
}
無需發佈孔類,如果你只需要幫助,一個方法.... –
爲'MULT(矩陣B的代碼)'是**不正確** ...... –
如果你看看[this](http://stackoverflow.com/help/how-to-ask)和[this](http:/ /stackoverflow.com/help/mcve) –